C# 为一个组合框而不是另一个组合框设置ComboBoxItems的样式?
我的风格是这样的:C# 为一个组合框而不是另一个组合框设置ComboBoxItems的样式?,c#,.net,wpf,C#,.net,Wpf,我的风格是这样的: var comboBoxItemStyle = new Style(typeof(ComboBoxItem)); comboBoxItemStyle.Setters.Add(new EventSetter(ComboBoxItem.PreviewMouseDownEvent, new MouseButtonEventHandler(OnMyComboItemMouseDown))); comboBoxItemStyle.S
var comboBoxItemStyle = new Style(typeof(ComboBoxItem));
comboBoxItemStyle.Setters.Add(new EventSetter(ComboBoxItem.PreviewMouseDownEvent,
new MouseButtonEventHandler(OnMyComboItemMouseDown)));
comboBoxItemStyle.Setters.Add(new EventSetter(ComboBoxItem.PreviewKeyDownEvent,
new KeyEventHandler(OnMyComboItemPreviewKeyDown)));
接下来我要做的是:
Resources.Add(typeof(ComboBoxItem), comboBoxItemStyle);
但这会将此样式添加到所有组合框中
假设我有两个组合框:
ComboBox myComboBox = new ComboBox();
ComboBox someOneElsesComboBox = new ComboBox();
如何将此样式应用于MyComboBox?当我点击“保存”时,我就明白了
myComboBox.Resources.Add(typeof(ComboBoxItem), comboBoxItemStyle);