WPF中ComboBox控件显示的样式设置
2016-11-03 16:47
609 查看
1、首先需要给ComboBox绑定一个集合
2、集合中的每一项是一个对象,比如是一个Student对象,需要在该类中添加一个静态DataTemplateKey类型的属性(必须是静态的)。如下:
3、然后在使用ComboBox的地方的添加资源
这样过后,ComboBox就不用设置DisplayMemberPath就可以看到显示的是Name,当然,这是简单的设置,可以在上面TextBlock的地方设置为复杂的控件。比如添加一些图片背景啥的,那么最后这些Option就是有图片背景啥的啦。
4、附录
其中ResourceKeyBinding是一个类
AlternationCount=”2” AlternatingRowBackground=”#E5E5E5”
2、集合中的每一项是一个对象,比如是一个Student对象,需要在该类中添加一个静态DataTemplateKey类型的属性(必须是静态的)。如下:
public class Student { private static DataTemplateKey _resourceKey string Name{get;set;} Int ID {get;set;} public static DataTemplateKey ResourceKey { get { return _resourceKey?? (_resourceKey= new DataTemplateKey(typeof(Student))); } } }
3、然后在使用ComboBox的地方的添加资源
<DataTemplate x:Key="{x:Static Member=paramodel:Student.ResourceKey }" DataType="paramodel:ResourceKey"> <ContentControl Content="{Binding}"> <ContentControl.Style> <Style TargetType="ContentControl"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate DataType="paramodel:ResourceKey"> <TextBlock Text="{parautil:ResourceKeyBinding Path=Name}" /> </DataTemplate> </Setter.Value> </Setter> </Style> </ContentControl.Style> </ContentControl> </DataTemplate>
这样过后,ComboBox就不用设置DisplayMemberPath就可以看到显示的是Name,当然,这是简单的设置,可以在上面TextBlock的地方设置为复杂的控件。比如添加一些图片背景啥的,那么最后这些Option就是有图片背景啥的啦。
4、附录
其中ResourceKeyBinding是一个类
public class ResourceKeyBindingExtension : MarkupExtension { public ResourceKeyBindingExtension() { } public ResourceKeyBindingExtension(string path) { Path = new PropertyPath(path); } [DefaultValue("")] public object AsyncState { get; set; } [DefaultValue(false)] public bool BindsDirectlyToSource { get; set; } [DefaultValue("")] public IValueConverter Converter { get; set; } [TypeConverter(typeof(CultureInfoIetfLanguageTagConverter))] [DefaultValue("")] public CultureInfo ConverterCulture { get; set; } [DefaultValue("")] public object ConverterParameter { get; set; } [DefaultValue("")] public string ElementName { get; set; } [DefaultValue("")] public PropertyPath Path { get; set; } [DefaultValue("")] public RelativeSource RelativeSource { get; set; } [DefaultValue("")] public object Source { get; set; } [DefaultValue("")] public string XPath { get; set; } [DefaultValue("")] public string StringFormat { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { var resourceKeyBinding = new Binding { BindsDirectlyToSource = BindsDirectlyToSource, Mode = BindingMode.OneWay, Path = Path, XPath = XPath, }; //Binding throws an InvalidOperationException if we try setting all three // of the following properties simultaneously: thus make sure we only set one if (ElementName != null) { resourceKeyBinding.ElementName = ElementName; } else if (RelativeSource != null) { resourceKeyBinding.RelativeSource = RelativeSource; } else if (Source != null) { resourceKeyBinding.Source = Source; } var targetElementBinding = new Binding { RelativeSource = new RelativeSource { Mode = RelativeSourceMode.Self } }; var multiBinding = new MultiBinding(); multiBinding.Bindings.Add(targetElementBinding); multiBinding.Bindings.Add(resourceKeyBinding); // If we set the Converter on resourceKeyBinding then, for some reason, // MultiBinding wants it to produce a value matching the Target Type of the MultiBinding // When it doesn't, it throws a wobbly and passes DependencyProperty.UnsetValue through // to our MultiBinding ValueConverter. To circumvent this, we do the value conversion ourselves. // See http://social.msdn.microsoft.com/forums/en-US/wpf/thread/af4a19b4-6617-4a25-9a61-ee47f4b67e3b multiBinding.Converter = new ResourceKeyToResourceConverter { ResourceKeyConverter = Converter, ConverterParameter = ConverterParameter, StringFormat = StringFormat, }; return multiBinding.ProvideValue(serviceProvider); } }
AlternationCount=”2” AlternatingRowBackground=”#E5E5E5”
相关文章推荐
- Flex中通过设定borderColor样式给ComboBox控件设置边框颜色的例子
- 继续聊WPF——设置网格控件列标题的样式
- ios之如何让图片显示成圆形的样式/设置控件边框大小以及颜色
- 设置控件全局显示样式 appearance
- Wpf 动态添加控件设置样式
- WPF后台设置xaml控件的样式System.Windows.Style
- Flex中通过设置fillColors样式改变ComboBox控件的填充背景颜色的例子
- wpf动态设置控件样式
- 设置控件全局显示样式appearance proxy
- 设置DevExpress GridControl控件时间列显示时、分、秒样式
- 让WPF的TextBox控件可以使用System.Windows.Forms.FontDialog设置样式
- Flex中如何通过设置fontFamily样式在ComboBox控件中使用自定义嵌入字体的例子
- 解决WinForm中ComboBox控件的“设置"DataSourse”属性后无法修改项集合”以及两个不相关联的ComboxBox控件实现数据列表显示不可实现的问题
- [转]设置控件全局显示样式appearance proxy
- WPF:DataTemplateSelector设置控件不同的样式
- 继续聊WPF——设置网格控件列标题的样式
- WPF:DataTemplateSelector设置控件不同的样式
- 设置控件全局显示样式appearance proxy
- 设置控件全局显示样式appearance proxy
- c#动态设置combobox控件下拉项宽度以实现下拉项文字可以完全显示。