【问题标题】:Associate Datatemplate with binding type [duplicate]将数据模板与绑定类型相关联[重复]
【发布时间】: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>

显示视图模型的正确值,但它采用默认数据模板

我不确定发生了什么?

【问题讨论】:

  • &lt;DataTrigger Binding="{Binding}" Value="LineRenderableSeriesViewModel"&gt; 如果当前项目的 (而不是类型)会以某种方式是 LineRenderableSeriesViewModel,则将起作用。而不是这个奇怪的结构,应该有一组 DataTemplate 资源,它们的 DataType 设置得当。不要明确设置 ItemTemplate 属性。或者,使用 DataTemplateSelector。

标签: c# wpf


【解决方案1】:

您可以轻松很多。
只需使用属性DataType 定义数据模板,绑定引擎会为您完成其余的工作。

<ListBox ItemsSource="{Binding Path=ACList}" >
  <ListBox.Resources>
    <DataTemplate DataType="{x:Type local:Other}" >
      <Border BorderBrush="Red" BorderThickness="2" Background="Pink">
        <TextBlock Text="{Binding}" Foreground="Red" FontWeight="Bold" Padding="5"/>
      </Border>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:LineRenderableSeriesViewModel}">
      <Border BorderBrush="Green" BorderThickness="2" CornerRadius="5">
        <TextBlock Text="I'm asssociated to LineRenderableSeriesViewModel " Foreground="Green" FontWeight="Bold" Padding="5"/>
      </Border>
    </DataTemplate>
  </ListBox.Resources>
</ListBox>

请记住将“本地”与您的类所在的命名空间定义进行交换

【讨论】:

  • 我确实改变了 但它仍然没有激活我认为类型是 DataBinding 或类似但不是我真正等待的那个我不知道如果类型是它应该是的那个,我不知道如何签入标签
  • 从 DataTemplate 中删除密钥,它应该可以工作
  • 是的,我使用了不同的技术,但它现在可以工作了,因为设置了一个键,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多