WPF中ComboBox控件显示的样式设置

1、首先需要给ComboBox绑定一个集合
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”

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值