WPF ComboBox项目的样式 [英] Styling WPF ComboBox items

查看:51
本文介绍了WPF ComboBox项目的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的WPF应用程序,它显示一个ComboBox,该ComboBox绑定到代表人的类列表。每个人物对象都有一个名称字符串字段和一个性别枚举。我希望ComboBox可以显示各个人的姓名字段的下拉列表,但是要根据性别字段设置每行的样式,例如,男性为蓝色,女性为粉色。有人可以告诉我我在做什么错吗?

I have a very simple WPF application which displays a ComboBox which binds to a list of classes which represent people. Each 'Person' object has a Name string field, and a Sex enum. I would like the ComboBox to display a drop-down of the various people's Name field, but for each line to be styled according to the Sex field, for example, blue for males, pink for females. Can anyone tell me what I am doing wrong?

以下是XML:

<Window x:Class="ComboBoxColour.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical">
        <ComboBox ItemsSource="{Binding People}" Width="100" Height="20">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Name="somePerson" Text="{Binding Path=Name}">                        
                        <TextBlock.Triggers>
                            <DataTrigger Binding="{Binding Path=Sex}" Value="Male">
                                <DataTrigger.Setters>
                                    <Setter Property="Foreground" Value="Blue" TargetName="somePerson" />
                                </DataTrigger.Setters>
                            </DataTrigger>
                        </TextBlock.Triggers>                        
                    </TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel>
</Window>

这是C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace ComboBoxColour
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public List<Person> people;
    public List<Person> People
    {
      get { return people; }
      set { people = value; }
    }

    public MainWindow()
    {
      this.DataContext = this;

      People = new List<Person>();
      People.Add(new Person("Alice", SexEnum.Female));
      People.Add(new Person("Bob", SexEnum.Male));
      People.Add(new Person("Claire", SexEnum.Female));
      People.Add(new Person("Daniel", SexEnum.Male));

      InitializeComponent();
    }
  }

  public enum SexEnum{Male,Female};

  public class Person
  {
    private string name;
    public string Name
    {
      get { return name; }
      set { name = value; }
    }

    private SexEnum sex;
    public SexEnum Sex
    {
      get { return sex; }
      set { sex = value; }
    }

    public Person(string Name, SexEnum Sex)
    {
      this.Name = Name;
      this.Sex = Sex;
    }
  }
}

在此先谢谢了

推荐答案

您应使用样式触发器而不是 TextBlock.Triggers

You should use "Style" triggers instead of "TextBlock.Triggers"

使用此XAML:

<Window x:Class="ComboBoxColour.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical">
        <ComboBox ItemsSource="{Binding People}" Width="100" Height="20">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Name="somePerson" Text="{Binding Path=Name}">
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=Sex}" Value="Male">
                                        <DataTrigger.Setters>
                                            <Setter Property="Foreground" Value="blue"/>
                                        </DataTrigger.Setters>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Sex}" Value="Female">
                                        <DataTrigger.Setters>
                                            <Setter Property="Foreground" Value="Pink"/>
                                        </DataTrigger.Setters>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel>
</Window>

现在您将拥有蓝色的男性和粉红色的女性。

Now you'll have blue for male and pink for female.

这篇关于WPF ComboBox项目的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆