Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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# 为组合框中的行添加颜色?_C#_Wpf_Combobox - Fatal编程技术网

C# 为组合框中的行添加颜色?

C# 为组合框中的行添加颜色?,c#,wpf,combobox,C#,Wpf,Combobox,我很难找到问题的解决方案,也就是说,我有一个想法,根据区域的不同,给combobox的每一行/每一列涂上不同的颜色,但我找不到任何线索、提示或说明。这个应用程序非常简单 <ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="84,70,0,0" VerticalAlignment="Top" Width="230"/> 这是我的XAML组合框,由代码填充: SortedList<int, stri

我很难找到问题的解决方案,也就是说,我有一个想法,根据区域的不同,给combobox的每一行/每一列涂上不同的颜色,但我找不到任何线索、提示或说明。这个应用程序非常简单

 <ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="84,70,0,0" VerticalAlignment="Top" Width="230"/>

这是我的XAML组合框,由代码填充:

SortedList<int, string> AreaList = new SortedList<int, string>();
        AreaList.Add(1, "Agriculture");
        AreaList.Add(2, "Forestry");
        AreaList.Add(3, "Fruits");
        AreaList.Add(4, "Food");
        AreaList.Add(5, "Metals");
        AreaList.Add(6, "Mining");
        AreaList.Add(7, "Electricity");
        AreaList.Add(8, "Building Contracts");
        AreaList.Add(9, "Transport");
        AreaList.Add(10, "Alcohol");
        AreaList.Add(11, "Information Technologies");
        AreaList.Add(12, "Health And Social Services");
        AreaList.Add(13, "Art and Entertainement");
        AreaList.Add(14, "Hospitality Business");
        AreaList.Add(15, "Education");
        AreaList.Add(16, "Real Estate");
        AreaList.Add(17, "Sales");
        AreaList.Add(18, "Architecture");
        AreaList.Add(19, "Engineering");
        AreaList.Add(20, "Wholesale");
        AreaList.Add(21, "Other");

        comboBox1.ItemsSource = AreaList.ToList();
        comboBox1.SelectedValuePath = "Key";
        comboBox1.DisplayMemberPath = "Value";
SortedList AreaList=新的SortedList();
区域列表。添加(1,“农业”);
区域列表。添加(2,“林业”);
区域列表。添加(3,“水果”);
区域列表。添加(4,“食品”);
区域列表。添加(5,“金属”);
区域列表。添加(6,“采矿”);
区域列表。添加(7,“电力”);
区域列表。添加(8,“建筑合同”);
区域列表。添加(9,“运输”);
区域列表。添加(10,“酒精”);
区域列表。添加(11,“信息技术”);
地区列表。添加(12,“卫生和社会服务”);
区域列表。添加(13,“艺术和娱乐”);
区域列表。添加(14,“酒店业务”);
区域列表。添加(15,“教育”);
区域列表。添加(16,“房地产”);
区域列表。添加(17,“销售”);
区域列表。添加(18,“架构”);
区域列表。添加(19,“工程”);
区域列表。添加(20,“批发”);
区域列表。添加(21,“其他”);
comboBox1.ItemsSource=AreaList.ToList();
comboBox1.SelectedValuePath=“Key”;
comboBox1.DisplayMemberPath=“值”;
每个项目在另一个窗口中都有它们的颜色,但我想在组合框中显示这些颜色,“农业”行/列的背景应该是绿色等。
有解决这个问题的方法吗,或者我必须重新做一遍吗?

您可以使用ItemContainerStyle和Converter

 public class StringToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (((KeyValuePair<int, string>)value).Value.ToString() == "Agriculture")
                return Brushes.Green;
            //and so on or other ways to get the color
            return Brushes.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
公共类StringToColorConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
if(((KeyValuePair)value.value.ToString()=“农业”)
返回笔刷。绿色;
//等等,或者其他获得颜色的方法
返回刷。透明;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
XAML如下所示

<Window.Resources>
        <local:StringToColorConverter x:Key="StringToColorConverter"/>
    </Window.Resources>
    <Grid >
        <ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="84,70,0,0" VerticalAlignment="Top" Width="230">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="Background" Value="{Binding Converter={StaticResource StringToColorConverter}}">

                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>
    </Grid>

您可以使用ItemContainerStyle和Converter

 public class StringToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (((KeyValuePair<int, string>)value).Value.ToString() == "Agriculture")
                return Brushes.Green;
            //and so on or other ways to get the color
            return Brushes.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
公共类StringToColorConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
if(((KeyValuePair)value.value.ToString()=“农业”)
返回笔刷。绿色;
//等等,或者其他获得颜色的方法
返回刷。透明;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
XAML如下所示

<Window.Resources>
        <local:StringToColorConverter x:Key="StringToColorConverter"/>
    </Window.Resources>
    <Grid >
        <ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="84,70,0,0" VerticalAlignment="Top" Width="230">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="Background" Value="{Binding Converter={StaticResource StringToColorConverter}}">

                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>
    </Grid>

您可以使用ItemContainerStyle,对映射到颜色的每个值使用DataTrigger:

<ComboBox x:Name="comboBox1">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Value}" Value="Agriculture">
                    <Setter Property="Background" Value="Green" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Value}" Value="Forestry">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
                <!-- and so on-->
            </Style.Triggers>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

您可能还想阅读以下内容:


更改WPF中组合框的背景色:

您可以使用ItemContainerStyle,并为映射到颜色的每个值使用DataTrigger:

<ComboBox x:Name="comboBox1">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Value}" Value="Agriculture">
                    <Setter Property="Background" Value="Green" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Value}" Value="Forestry">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
                <!-- and so on-->
            </Style.Triggers>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

您可能还想阅读以下内容:


在WPF中更改组合框的背景颜色:

事实上就是这样,tnx很多。为此,我还在学习WPF,我可以在代码中找到自己的方法,但XAML仍然是一个迷,tnx表示耐心:)哦,我突然想起,作为将来的参考,是否可以在结尾添加一个正方形,例如,农柱,绿色,方正?如果这个问题很愚蠢,你不必回答。事实上,就是这样,tnx很多。我还在学习WPF,我可以在代码中找到自己的方法,但XAML仍然是一个迷雾,tnx表示耐心:)哦,我突然想到,为了将来的参考,有没有可能在例如农业专栏的末尾添加一个正方形,绿色,就在广场上?如果问题很愚蠢,你不必回答xD