Upon dynamically making a TreeViewItem
in code, I get binding errors relating to content alignment, even though the associated style specifies this.
TreeViewItem tvi = new TreeViewItem()
{
Header = "aString",
Style = wpfElement.FindResource("tviRoot") as Style
};
The error I get is
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'TreeViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
(and a similar one for the vertical alignment)
I don't really understand why I get this error and I can't figure it out (I'm not that well-versed in WPF). The weird thing is that everything seems to work as expected, but is still massively slowed down by these errors. Can somebody help?
Edit: I had given the style property a stub name, as I didn't think it mattered, I've renamed it and included its definition below: tviRoot
, based on 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>
Okay, changing the order of the creation arguments got rid of the error. (I guess it set the TreeViewItem
's Header
attribute before associating a Style
, and that's what was causing the problem..?)
I got a lot more of these errors in my application, so I can't see yet if some new ones didn't pop up somewhere else.