【发布时间】:2018-09-28 15:02:13
【问题描述】:
我正在尝试根据分配给 ComboBox ItemSource 的属性值更改 ComboBoxItem 的值。
我知道在WPF中可以实现如下:
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Background" Value="#FFD2D2" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsValid}" Value="True">
<Setter Property="Background" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
在 UWP 中,我尝试使用行为,但仍然无法正常工作。
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Background" Value="Transparent" />
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior Binding="{Binding IsValid}" Value="True">
<core:ChangePropertyAction PropertyName="Background" Value="{ThemeResource MyBorderBrush}" />
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Style>
</ComboBox.ItemContainerStyle>
我也尝试过使用 VSM,但不确定如何应用条件值。
<Style TargetType="ComboBoxItem">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Grid x:Name="LayoutRoot" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
// What should go here?
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
编辑:
我更喜欢设置 ComboBoxItem 本身背景的解决方案,而不是创建单独的网格/边框,然后使用转换器作为背景。
【问题讨论】:
-
@Muzib,从我的帖子中,您可以看到我已经尝试过这种方式,但它不起作用。