C# Wpf 将服装样式设置为 ComboBoxItem

我有ComboBox一个列表。该ComboBox列表中有 3 个 ComboBoxItems。我也有一个CheckBox。如果CheckBox未选中并且选择了第一个 ComboBoxItem,则我的视图模型中的属性“Property1”设置为 false。现在,如果“Property1”为假,我想更改此 ComboBoxItem 的背景和前景。我怎样才能做到这一点?


我试图用 Style 和 MultiDataTrigger 来完成这项工作,但没有成功——我只设法更改了所有 ComboBoxItems 的 Style 而不是特定的一个。


<Style TargetType="ComboBoxItem">

            <Style.Triggers>

               <MultiDataTrigger>

                   <MultiDataTrigger.Conditions>

                        <Condition Binding="{Binding Path=SelectedComboBoxItem}" Value="Item1" />

                       <Condition Binding="{Binding Path=CheckStatus}" Value="False" />

                   </MultiDataTrigger.Conditions>

                   <Setter Property="Background" Value="GhostWhite" />

                   <Setter Property="Foreground" Value="Gainsboro" />

               </MultiDataTrigger>

            </Style.Triggers>

        </Style>

通过 List 绑定时,如何更改特定 ComboBoxItem 的样式?


狐的传说
浏览 241回答 1
1回答

慕容708150

这可能不是最好的解决方案,而是一个可行的解决方案:我没有使用样式,而是使用 ItemTemplate 创建了一个组合框&nbsp; &nbsp; &nbsp; &nbsp; <ComboBox Width="200" Height="30"&nbsp; ItemsSource="{Binding SimpleList, UpdateSourceTrigger=PropertyChanged}" >&nbsp; &nbsp; &nbsp; &nbsp; <ComboBox.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Label Content="{Binding Name}" Background="{Binding BackGround}" Foreground="{Binding ForeGround}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; </ComboBox.ItemTemplate>&nbsp; &nbsp; </ComboBox>我的 SimpleList 包含 3 个具有名称、背景和前景属性的“简单对象”。BackGround 默认设置为白色,ForeGround 默认设置为黑色。一旦我的 CheckBox 被选中,我就会得到我的 List 的第一个 Item 并更改它的属性。&nbsp; &nbsp; private void CheckBox_Checked(object sender, RoutedEventArgs e){&nbsp; SimpleObject obj = SimpleList.FirstOrDefault();&nbsp; obj.BackGround = new SolidColorBrush(Colors.Black);&nbsp; obj.ForeGround = new SolidColorBrush(Colors.White);}
打开App,查看更多内容
随时随地看视频慕课网APP