[英]WPF Binding error on creating an element in C# code
在代碼中動態制作TreeViewItem
時,即使相關聯的樣式指定了此內容,也會出現與內容對齊有關的綁定錯誤。
TreeViewItem tvi = new TreeViewItem()
{
Header = "aString",
Style = wpfElement.FindResource("tviRoot") as Style
};
我得到的錯誤是
System.Windows.Data信息:10:無法使用綁定來檢索值,並且不存在有效的后備值;請參閱參考資料。 使用默認值代替。 BindingExpression:Path = HorizontalContentAlignment; 的DataItem = NULL; 目標元素是'TreeViewItem'(Name =''); 目標屬性為“ 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>
好的,更改創建參數的順序消除了錯誤。 (我猜想它在關聯Style
之前設置了TreeViewItem
的Header
屬性,這就是引起問題的原因..?)我在應用程序中遇到了很多這樣的錯誤,所以我看不到是否有一些新錯誤沒有在其他地方彈出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.