【问题标题】:Styling WPF ComboBox items样式化 WPF ComboBox 项目
【发布时间】:2012-01-05 16:49:13
【问题描述】:

我有一个非常简单的 WPF 应用程序,它显示一个 ComboBox,它绑定到代表人的类列表。每个“Person”对象都有一个 Name 字符串字段和一个 Sex 枚举。我希望 ComboBox 显示各种人的 Name 字段的下拉列表,但要根据 Sex 字段设置每一行的样式,例如,男性为蓝色,女性为粉红色。谁能告诉我我做错了什么?

这是 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;
    }
  }
}

在此先感谢

【问题讨论】:

    标签: c# wpf combobox datatemplate styling


    【解决方案1】:

    使用ItemContainerStyle 代替ItemTemplate

        <ComboBox ItemsSource="{Binding People}" Width="100" Height="20">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="Foreground" Value="Pink" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Sex}" Value="Male">
                            <Setter Property="Foreground" Value="Blue" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>
    

    【讨论】:

      【解决方案2】:

      您应该使用“样式”触发器而不是“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>
      

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

      【讨论】:

        猜你喜欢
        • 2018-06-07
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        • 2011-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多