在C#代码中创建元素时出现WPF绑定错误

本文关键字:WPF 绑定 错误 元素 代码 创建 | 更新日期: 2023-09-27 18:27:03

在代码中动态生成TreeViewItem时,我会收到与内容对齐有关的绑定错误,即使相关的样式指定了这一点。

TreeViewItem tvi = new TreeViewItem() 
{
    Header = "aString", 
    Style = wpfElement.FindResource("tviRoot") as Style 
};

我得到的错误是

System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值;使用默认值。BindingExpression:Path=HorizontalContentAlignment;DataItem=null;目标元素是"TreeViewItem"(名称=");目标属性是"HorizontalContentAlignment"(类型为"HorizontalAlignment)

(以及用于垂直对齐的类似)

我真的不明白为什么我会犯这个错误,我也弄不明白(我对WPF不太熟悉)。奇怪的是,一切似乎都如预期的那样运转,但仍因这些错误而大幅放缓。有人能帮忙吗?

编辑:我给样式属性起了一个存根名称,因为我认为这无关紧要,所以我重命名了它,并在下面包含了它的定义:tviRoot,基于tviBaseStyle

<Style TargetType="TreeViewItem" x:Key="tviBaseStyle">
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeViewItem">
                <Grid Margin="0">
                    <Grid.RowDefinitions>
                        <!--The top row contains the item's content.-->
                        <RowDefinition Height="{StaticResource rowHeight}" />
                        <!--The bottom row contains the item's children.-->
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <!-- This Border and ContentPresenter displays the content of the TreeViewItem. -->
                    <Border Name="Bd" Margin="0" Padding="0"
                            Background="White" BorderBrush="Gray" BorderThickness="1" CornerRadius="2"
                            TextElement.FontSize="{StaticResource fontSize}"
                            TextElement.FontFamily="{StaticResource fontFamily}">
                        <ContentPresenter ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                    <!-- The ItemsPresenter displays the item's children. -->
                    <ItemsPresenter Grid.Row="1"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Bd" Property="Border.BorderBrush" Value="Red" />
                        <Setter TargetName="Bd" Property="Border.BorderThickness" Value="1"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <!-- Make each TreeViewItem show its children in a horizontal StackPanel. -->
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel HorizontalAlignment="Center" IsItemsHost="True" Margin="0" Orientation="Horizontal"  />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="tviRoot" TargetType="TreeViewItem" BasedOn="{StaticResource tviBaseStyle}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeViewItem">
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <!--The top row contains the item's content.-->
                        <RowDefinition Height="{StaticResource rowHeight}" />
                        <!--The bottom row contains the item's children.-->
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <!-- This Border and ContentPresenter displays the content of the TreeViewItem. -->
                    <Border Name="Bd" Margin="0" Padding="0"
                            Background="{StaticResource rootGradient}" BorderBrush="Black" BorderThickness="2" CornerRadius="2"
                            TextElement.FontSize="{StaticResource fontSize}"
                            TextElement.FontWeight="Bold"
                            TextElement.FontFamily="{StaticResource fontFamily}">
                        <ContentPresenter ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <!-- The ItemsPresenter displays the item's children. -->
                    <ItemsPresenter Grid.Row="1"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Bd" Property="Border.BorderBrush" Value="Red" />
                        <Setter TargetName="Bd" Property="Border.Background" Value="{StaticResource rootGradientSelected}"/>
                        <Setter TargetName="Bd" Property="TextElement.Foreground" Value="Yellow"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <!-- Make each TreeViewItem show its children in a horizontal StackPanel. -->
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel HorizontalAlignment="Center" IsItemsHost="True" Margin="0" Orientation="Horizontal"  />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

在C#代码中创建元素时出现WPF绑定错误

好的,更改创建参数的顺序可以消除错误。(我猜它在关联Style之前设置了TreeViewItemHeader属性,这就是导致问题的原因。?)我的应用程序中出现了更多这样的错误,所以我还不知道是否有新的错误没有出现在其他地方。