Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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
如何对wpf组合框项目进行分类(与可观察集合绑定)&;每个类别必须显示不同的背景色_Wpf - Fatal编程技术网

如何对wpf组合框项目进行分类(与可观察集合绑定)&;每个类别必须显示不同的背景色

如何对wpf组合框项目进行分类(与可观察集合绑定)&;每个类别必须显示不同的背景色,wpf,Wpf,我是WPF新手,我有一个带有MVVM模式+Prism的WPF应用程序,我有一个绑定了可观察集合类型的组合框 帐户有:Id(主键),AccountName 我已经建立了一些帐户(为用户内置),ID从8000到10000,其余的正在生成,通过应用程序,>10000,错误地创建了一些ID10000)和(1到账户); } } } 私人ListCollectionView_账户; 公共主视图模型() { var accounts=新列表(); accounts.Add(新帐户(){Id=1,Account

我是WPF新手,我有一个带有MVVM模式+Prism的WPF应用程序,我有一个绑定了可观察集合类型的组合框

帐户有:Id(主键),AccountName

我已经建立了一些帐户(为用户内置),ID从8000到10000,其余的正在生成,通过应用程序,>10000,错误地创建了一些ID<8000的帐户(不可编辑为正在运行的应用程序)

现在我需要根据Id组(8000到10000)和(Id>10000)和(1到<8000)对组合框中的项目进行分类,并且还希望这些类别在组合框中显示不同的背景

提前谢谢

您可以使用组合框和数据模板选择器对项目进行分组,以更改分组中的背景颜色

组合框中的分组:请参见或

数据模板选择器:请参见或

例如:

MainWindow.xaml

<Window x:Class="ComboboxGroups.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"
    xmlns:Local="clr-namespace:ComboboxGroups">

<Window.Resources>
    <DataTemplate x:Key="tmpl1">
        <TextBlock Text="{Binding Name}" Background="Green" />
    </DataTemplate>

    <DataTemplate x:Key="tmpl2">
        <TextBlock Text="{Binding Name}" Background="Red" />
    </DataTemplate>

    <DataTemplate x:Key="tmpl3">
        <TextBlock Text="{Binding Name}" Background="Yellow"/>
    </DataTemplate>

    <Local:TemplateSelector x:Key="tmplselector"
        Tmpl1="{StaticResource tmpl1}" Tmpl2="{StaticResource tmpl2}" Tmpl3="{StaticResource tmpl3}" />
</Window.Resources>

<StackPanel>
    <ComboBox ItemsSource="{Binding Accounts}" DisplayMemberPath="Account.AccountName">
        <ComboBox.GroupStyle>
            <GroupStyle HeaderTemplateSelector="{StaticResource tmplselector}" />                    
        </ComboBox.GroupStyle>
    </ComboBox>
</StackPanel>

视图模型和实体类型:

public class Account
{
    public int Id { get; set; }
    public string AccountName { get; set; }
}

public class ComboboxGroup<TItem>
{
    public string CategoryName { get; set; }        
    public TItem Account { get; set; }
}

public class MainViewModel : ViewModelBase
{
    public ListCollectionView Accounts
    {
        get { return _Accounts; }
        set
        {
            if (_Accounts != value)
            {
                _Accounts = value;
                RaisePropertyChanged(() => Accounts);
            }
        }
    }
    private ListCollectionView _Accounts;

    public MainViewModel()
    {
        var accounts = new List<Account>();
        accounts.Add(new Account() { Id = 1, AccountName = "Acount 1"});
        accounts.Add(new Account() { Id = 2, AccountName = "Acount 2" });
        accounts.Add(new Account() { Id = 9000, AccountName = "Acount 9000" });
        accounts.Add(new Account() { Id = 9001, AccountName = "Acount 9001" });
        accounts.Add(new Account() { Id = 10000, AccountName = "Acount 10000" });
        accounts.Add(new Account() { Id = 10001, AccountName = "Acount 10002" });

        var groups = new List<ComboboxGroup<Account>>();

        foreach (var account in accounts)
        {
            if (account.Id >= 1 && account.Id < 8000)                
                groups.Add(new ComboboxGroup<Account>() { Account = account, CategoryName = "<1;8000)"});                
            else if (account.Id >= 8000 && account.Id <= 10000)                
                groups.Add(new ComboboxGroup<Account>() { Account = account, CategoryName = "<8000;10000>" });                
            else                
                groups.Add(new ComboboxGroup<Account>() { Account = account, CategoryName = "(10000; infinity)" });                
        }

        Accounts = new ListCollectionView(groups);
        Accounts.GroupDescriptions.Add(new PropertyGroupDescription("CategoryName"));
    }        
}
公共类帐户
{
公共int Id{get;set;}
公共字符串AccountName{get;set;}
}
公共类ComboxGroup
{
公共字符串CategoryName{get;set;}
公共滴度帐户{get;set;}
}
公共类MainViewModel:ViewModelBase
{
公共ListCollectionView帐户
{
获取{return\u Accounts;}
设置
{
如果(_Accounts!=值)
{
_账户=价值;
RaisePropertyChanged(()=>账户);
}
}
}
私人ListCollectionView_账户;
公共主视图模型()
{
var accounts=新列表();
accounts.Add(新帐户(){Id=1,AccountName=“Acount 1”});
accounts.Add(新帐户(){Id=2,AccountName=“Acount 2”});
accounts.Add(新帐户(){Id=9000,AccountName=“Acount 9000”});
accounts.Add(新帐户(){Id=9001,AccountName=“Acount 9001”});
accounts.Add(新帐户(){Id=10000,AccountName=“Acount 10000”});
accounts.Add(新帐户(){Id=10001,AccountName=“Acount 10002”});
变量组=新列表();
foreach(账户中的var账户)
{
如果(account.Id>=1&&account.Id<8000)

groups.Add(new ComboboxGroup(){Account=Account,CategoryName=“感谢'Jan Smuda'的快速回答,是的,这解决了我的问题,但我用了另一种方法! 我使用了可观察收集

帐目

,命名为“\u EntityCollection”,用于与Combobox绑定,我使用Converter对帐户进行分组:

 public class AccountIdConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var v = value as int?;

        if (v == null)
            return value;

        return Convert(v.Value);
    }

    public static string Convert(int id)
    {
        if (id >= 1 && id <= 8000)
            return "1-8000";
        else if (id >= 8001 && id <= 10000)
            return "8001-10000";


            return "> 10001";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
公共类AccountIdConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
var v=作为int?的值?;
如果(v==null)
返回值;
返回转换(v.Value);
}
公共静态字符串转换(int-id)
{
如果(id>=1&&id=8001&&id

是的,这很好地解决了我的问题!所有项目都在不同的类别中,在具有不同背景颜色的扩展器下。

您的意思是ComboBox中的记录将超过10000条,还是ComboBox中的三个类别1)实际上是1)
 public class AccountIdConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var v = value as int?;

        if (v == null)
            return value;

        return Convert(v.Value);
    }

    public static string Convert(int id)
    {
        if (id >= 1 && id <= 8000)
            return "1-8000";
        else if (id >= 8001 && id <= 10000)
            return "8001-10000";


            return "> 10001";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<UserControl.Resources>
    <coverter:AccountIdConverter x:Key="AccIdConverter" />

    <!--Create CollectionViewSource and set the property we want to group by-->
    <CollectionViewSource x:Key="MyItems" Source="{Binding _EntityCollection}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Id"  Converter="{StaticResource AccIdConverter}"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</UserControl.Resources>





  <ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource MyItems},Mode=OneWay}"  SelectedItem="{Binding SelectedEntity, Mode=TwoWay}" DisplayMemberPath="AccountName" SelectedIndex="{Binding Path=SelectedItemIndex,Mode=TwoWay}" Width="250" Margin="10,5,0,5">

        <ComboBox.GroupStyle>
            <!-- Style for groups at top level. -->
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True" BorderBrush="#FF002255" BorderThickness="1,1,1,5">
                                        <Expander.Header>
                                            <DockPanel>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100"/>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                        <Expander.Style>
                                            <Style TargetType="Expander">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=GroupItem}, Path=DataContext.Name}" Value="1-8000">
                                                        <Setter Property="Background" Value="#FF002255" />
                                                        <Setter Property="Foreground" Value="#FFEEEEEE" />
                                                    </DataTrigger>

                                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=GroupItem}, Path=DataContext.Name}" Value="8001-10000">
                                                        <Setter Property="Background" Value="AliceBlue"/>
                                                        <Setter Property="Foreground" Value="Maroon" />
                                                    </DataTrigger>

                                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=GroupItem}, Path=DataContext.Name}" Value="> 10001">
                                                        <Setter Property="Background" Value="Yellow"/>
                                                        <Setter Property="Foreground" Value="Red" />
                                                    </DataTrigger>

                                                </Style.Triggers>
                                            </Style>
                                        </Expander.Style>

                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>             

        </ComboBox.GroupStyle>
    </ComboBox>