【发布时间】:2021-10-03 21:19:03
【问题描述】:
我有一个绑定到 ItemsSource 的 ItemsControl。每个项目都可以根据项目上各种属性的值分配多个 DataTemplate 之一。这些属性可以在运行时更改,并且需要单独换出 DataTemplates。在 WPF 中,我可以使用以下(部分简化的 xaml)来做到这一点:
<ItemsControl
ItemsSource="{Binding Items}">
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="ContentTemplate">
<Setter.Value>
<MultiBinding Converter="{StaticResource RowTemplateConverter}">
<Binding Path="(local:Row.Sum)" />
<Binding Path="(local:Row.Avg)" />
<Binding Path="(local:Row.ShowFlagA)" />
<Binding Path="(local:Row.ShowFlagB)" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
我在尝试将其移至 UWP 时遇到了几个问题:
- 不支持多重绑定
- 为了弥补上述问题,我尝试将转换器逻辑整合到 Row 的单个字符串属性中,但似乎没有分配 DataTemplate。我使用的绑定语法也会导致运行时 XAML 错误,不知道为什么。
<ItemsControl
ItemsSource="{Binding Items}" >
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="ContentTemplate">
<Setter.Value>
<Binding Path="RowTemplate" Converter="{StaticResource RowTemplateConverter}"/>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
- DataTemplateSelector 和 ItemContainerStyleSelector 不起作用,因为它们只评估一次,我需要根据各种属性更改更新模板
- 我在这里看到了一些答案,说将上述选择器设为空并重新分配它们。这是我能达到的最接近我想要的行为,但是性能很差,有几十个项目,而且属性变化很快,所以我无法使用它。
【问题讨论】:
-
绑定在 UWP 样式设置器中不起作用。您可以尝试这样的方法:stackoverflow.com/a/33582406/1136211
标签: c# .net wpf data-binding uwp