首页 / C# / c#-不同的选定项目的WPF组合框背景
c#-不同的选定项目的WPF组合框背景
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-不同的选定项目的WPF组合框背景,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1996字,纯文字阅读大概需要3分钟。
内容图文
![c#-不同的选定项目的WPF组合框背景](/upload/InfoBanner/zyjiaocheng/685/a51e5299be914be588a34498187fdec0.jpg)
我的Combobox只有3个项目:Planing,Progress和Done,
<ComboBox SelectedIndex="0>
<ComboBoxItem Content="Planing"/>
<ComboBoxItem Content="Progress"/>
<ComboBoxItem Content="Done"/>
</ComboBox>
我如何更改ComboBox的背景颜色(由defult Gradiant设计)取决于选择了哪个项目.
例如:紫色表示“ Planing”,蓝色表示“ Progress”,绿色表示“ Done”.
注意:我的意思是ComboBox背景,而不是ComboBox项目列表.
谢谢
解决方法:
1)使用selectionChanged事件
您可以在comboBox_SelectionChanged事件中进行设置
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBox.SelectedItem.ToString() == "Planning")
{
comboBox.Background = Brushes.Purple;
}
else if (comboBox.SelectedItem.ToString() == "Progress")
{
comboBox.Background = Brushes.Blue;
}
else if (comboBox.SelectedItem.ToString() == "Done")
{
comboBox.Background = Brushes.Green;
}
}
每次更改组合框中的选定值时,都会调用comboBox_SelectionChanged事件.在其中,您可以简单地验证所选项目的值并应用所需的颜色.
这将是Combobox的xaml
<ComboBox x:Name="comboBox" SelectionChanged="comboBox_SelectionChanged"/>
2)在XAML中使用DataTriggers
也可以通过在Style上设置多个DataTrigger来通过xaml完成.
<ComboBox x:Name="mycombobox">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedItem.Content, ElementName=mycombobox}" Value="Planning">
<Setter Property="Background" Value="Purple" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=SelectedItem.Content, ElementName=mycombobox}" Value="Progress">
<Setter Property="Background" Value="Blue" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=SelectedItem.Content, ElementName=mycombobox}" Value="Done">
<Setter Property="Background" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBoxItem Content="Planning"/>
<ComboBoxItem Content="Progress"/>
<ComboBoxItem Content="Done"/>
</ComboBox>
More information on DataTriggers:
Represents a trigger that applies property values or performs actions when the bound data meets a specified condition.
内容总结
以上是互联网集市为您收集整理的c#-不同的选定项目的WPF组合框背景全部内容,希望文章能够帮你解决c#-不同的选定项目的WPF组合框背景所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。