코드에서 TreeViewItem
을 동적으로 만들면 연관된 스타일이이를 지정하더라도 콘텐츠 맞춤과 관련된 바인딩 오류가 발생합니다.WPF C# 코드에서 요소를 만드는 중 바인딩 오류가 발생했습니다.
TreeViewItem tvi = new TreeViewItem()
{
Header = "aString",
Style = wpfElement.FindResource("tviRoot") as Style
};
내가 오류가
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')
(수직 정렬을위한 유사한 일) 나는이 오류가 왜 난 정말 이해가 안
내가 알아낼 수 없습니다 그것을 밖으로 (나는 WPF에 정통하지 않다.). 기이 한 일은 모든 것이 예상대로 작동하는 것처럼 보이지만이 오류로 인해 여전히 속도가 느려집니다. 누군가 도울 수 있습니까?
편집 : 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>
바인딩을 사용하고있는 곳에서 myStyle에 대한 코드를 게시 할 수 있습니까? –
나를 위해,이 스타일은 잘 작동합니다. 트리에 항목을 추가하려는 위치는 어디입니까? 트리가 제대로로드되지 않았을 수 있습니까? –
예, 스타일이 잘 작동합니다. 문제는 프로그래밍 방식으로 새 TreeViewItem을 만들려고 할 때 발생합니다 (자체적으로 아직 다른 것과 연결되지 않음). 이상한 점은 생성 코드를 다음과 같이 변경해도 오류가 제거되지 않는다는 것입니다. 'TreeViewItem tvi = new TreeViewItem() {Header = "aString", Style = wpfTreeView.FindResource ("tviRoot") 스타일로, HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch}; ' – RobV