在C#代码中创建元素的WPF绑定错误

时间:2013-08-10 14:23:13

标签: c# wpf binding treeviewitem

在代码中动态生成TreeViewItem后,即使关联的样式指定了此内容,我也会收到与内容对齐相关的绑定错误。

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

我得到的错误是

  

System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值;使用默认值。 BindingExpression:路径= Horizo​​ntalContentAlignment;的DataItem = NULL; target元素是'TreeViewItem'(Name =''); target属性是'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>

1 个答案:

答案 0 :(得分:0)

好的,更改创建参数的顺序可以消除错误。 (我想它在关联TreeViewItem之前设置了Header的{​​{1}}属性,这就是造成问题的原因..?) 我在我的应用程序中遇到了很多这些错误,所以我还不知道是否有一些新的错误没有弹出其他地方。