2

在动态创建TreeViewItem代码时,我得到与内容对齐相关的绑定错误,即使关联的样式指定了这一点。

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

我得到的错误是

System.Windows.Data 信息:10:无法使用绑定检索值,并且不存在有效的备用值;改用默认值。BindingExpression:Path=Horizo​​ntalContentAlignment; 数据项=空;目标元素是'TreeViewItem'(名称='');目标属性是“Horizo​​ntalContentAlignment”(类型“Horizo​​ntalAlignment”)

(和一个类似的垂直对齐)

我真的不明白为什么我会收到这个错误并且我无法弄清楚(我对 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>
4

1 回答 1

0

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

于 2013-08-10T15:54:10.463 回答