您的位置:首页 > Web前端 > CSS

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

2016-11-03 16:47 609 查看
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”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  wpf