【发布时间】:2021-07-05 12:08:45
【问题描述】:
即使绑定类型似乎是 LineRenderableSeriesViewModel,我的代码也始终显示默认数据模板。
<ListBox ItemsSource="{Binding ElementName=ACList, Path=SelectedItem.seriesViewModels, Mode=OneWay}" IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="Default" >
<Border BorderBrush="Red" BorderThickness="2" Background="Pink">
<TextBlock Text="{Binding}" Foreground="Red" FontWeight="Bold" Padding="5"/>
</Border>
</DataTemplate>
<DataTemplate x:Key="BindedTemplate1" >
<Border BorderBrush="Green" BorderThickness="2" CornerRadius="5">
<TextBlock Text="I'm asssociated to LineRenderableSeriesViewModel " Foreground="Green" FontWeight="Bold" Padding="5"/>
</Border>
</DataTemplate>
</StackPanel.Resources>
<!-- atempt to write type of object -->
<Label Content="{Binding}" ContentStringFormat="MyBindingType : {0}"></Label>
<!-- atempt to display the associated template -->
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource Default}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="LineRenderableSeriesViewModel">
<Setter Property="ContentTemplate" Value="{StaticResource BindedTemplate1}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我不确定发生了什么?
【问题讨论】:
-
<DataTrigger Binding="{Binding}" Value="LineRenderableSeriesViewModel">如果当前项目的 值(而不是类型)会以某种方式是 LineRenderableSeriesViewModel,则将起作用。而不是这个奇怪的结构,应该有一组 DataTemplate 资源,它们的 DataType 设置得当。不要明确设置 ItemTemplate 属性。或者,使用 DataTemplateSelector。