Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置WPF组合框项目的样式_C#_Wpf_Combobox_Datatemplate_Styling - Fatal编程技术网

C# 设置WPF组合框项目的样式

C# 设置WPF组合框项目的样式,c#,wpf,combobox,datatemplate,styling,C#,Wpf,Combobox,Datatemplate,Styling,我有一个非常简单的WPF应用程序,它显示一个组合框,该组合框绑定到表示人的类列表。每个“Person”对象都有一个名称字符串字段和一个性别枚举。我希望ComboBox显示各种人名字段的下拉列表,但每一行都要根据性别字段设置样式,例如,蓝色代表男性,粉色代表女性。谁能告诉我我做错了什么 以下是XML: <Window x:Class="ComboBoxColour.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20

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

以下是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#:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Collections.ObjectModel;
名称空间组合框颜色
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公众人物名单;
公众人物名单
{
获取{返回人;}
设置{people=value;}
}
公共主窗口()
{
this.DataContext=this;
人员=新列表();
添加(新人物(“Alice”,SexEnum.femal));
添加(新人物(“Bob”,SexEnum.Male));
添加(新人物(“克莱尔”,SexEnum.Female));
添加(新人物(“Daniel”,SexEnum.Male));
初始化组件();
}
}
公共enum SexEnum{男性,女性};
公共阶层人士
{
私有字符串名称;
公共字符串名
{
获取{返回名称;}
设置{name=value;}
}
私人性行为;
公共性
{
获取{返回性;}
设置{sex=value;}
}
公众人物(字符串名称,SexEnum Sex)
{
this.Name=Name;
这个。性=性;
}
}
}
提前多谢

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


现在,蓝色代表男性,粉色代表女性。

使用
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>

    <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>