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# 如何将网格添加到我的组合框并显示obervableCollection数据?_C#_Wpf_Mvvm - Fatal编程技术网

C# 如何将网格添加到我的组合框并显示obervableCollection数据?

C# 如何将网格添加到我的组合框并显示obervableCollection数据?,c#,wpf,mvvm,C#,Wpf,Mvvm,我使用一个组合框,我希望按照以下步骤进行操作: 我在cmb1中选择一个元素,这允许在cmb2中显示一个集合 此代码允许我检索所需的数据(结果A=ObservableCollectionA,结果B=ObservableCollectionB…) 现在,我想将我的组合框划分为三个网格,以便我可以按如下方式进行操作: 如果选择结果A=>cmb2 grid0=ObservableCollectionA.ID,cmb2 grid1=ObservableCollectionA.Name 如果选择结果B=

我使用一个组合框,我希望按照以下步骤进行操作: 我在cmb1中选择一个元素,这允许在cmb2中显示一个集合

此代码允许我检索所需的数据(结果A=ObservableCollectionA,结果B=ObservableCollectionB…)


现在,我想将我的组合框划分为三个网格,以便我可以按如下方式进行操作:

如果选择结果A=>cmb2 grid0=ObservableCollectionA.ID,cmb2 grid1=ObservableCollectionA.Name

如果选择结果B=>cmb2 grid0=ObservableCollectionB.Name,cmb2 grid1=ObservableCollectionB.Years

我不知道该怎么做

有什么建议吗

谢谢你的帮助

编辑:

c#代码:

private observeCollection\u etablishementsutilitisator;
公共可观测收集ETABILISEMENTSULISATEUR
{
得到
{
返回_etablisementsutilisator;
}
设置
{
如果(值!=\u etablishementsutilitisator)
{
_ETABILISEMENTSULISISATEUR=价值;
RaisePropertyChanged(名称(ETABILISEMENTSULISATEUR));
}
}
}
私人可观测收集服务;
公共收集服务
{
得到
{
退货服务;
}
设置
{
如果(值!=\u服务)
{
_服务=价值;
RaiseProperty变更(服务名称);
}
}
}
私人可观测采集部分;
公共收集组
{
得到
{
返回部分;
}
设置
{
如果(值!=\u节)
{
_截面=值;
RaiseProperty变更(部门名称);
}
}
}         
私有字符串_SelectedChoiceList;
公共字符串选择选项列表
{
得到
{
return\u SelectedChoiceList;
}
设置
{
如果(值!=\u SelectedChoiceList)
{
_SelectedChoiceList=值;
RaisePropertyChanged(名称(SelectedChoiceList));
}
}
}            
etablishements=newobserveCollection((wait _dataService.getetablishements().configurewait(false));
Services=await\u dataService.GetServicesAsync(false).ConfigureAwait(false);
Sections=await\u dataService.GetSectionsAsync(\u dataService.ParamGlobaux.IDEtablissement).ConfigureAwait(false);
包含ID、姓名、年份

服务包含颜色、ID、名称

节包含颜色、ID、节名称

编辑2:我想要这样的例子:

        <ComboBox Name="CbService" HorizontalAlignment="Left" Margin="115,67,0,0" VerticalAlignment="Top" Width="150" ItemsSource="{Binding}" SelectionChanged="CbRecherche_SelectionChanged" KeyboardNavigation.TabIndex="1" >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Libelle}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid x:Name="gd" TextElement.Foreground="Black" Background="White">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition  Width="50"/>
                                    <ColumnDefinition Width="Auto" MinWidth="50" />
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Rectangle Fill="{Binding Fond}" Grid.Column="0"/>
                                <TextBlock Margin="5" Grid.Column="0" Text="{Binding ID}"/>
                                <TextBlock Margin="5" Grid.Column="1" Text="{Binding Libelle}"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>

当前我的组合框显示一个字符串。我想要这样的东西:


在本例中,有一个ID、仅ID部分中的颜色和一个名称。我现在不能用我的字符串来做这件事。

我相信你可以通过删除RaisePropertyChanged事件来减少代码的大小,因为ObservableCollections已经包含INotifyPropertyChanged接口。我举了一个简单的例子,说明了如何使用Datatemplate来显示ObservableCollections中的信息

步骤1:C#代码:

名称空间WpfApp1
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
ComboBox1.ItemsSource=服务;
添加(新ServiceSectionModel{Color=brusks.Red,ID=“Clem”,Name=“Clementine”});
添加(新ServiceSectionModel{Color=brusks.White,ID=“011”,Name=“Logistique”});
添加(新ServiceSectionModel{Color=Brusks.Green,ID=“MBT”,Name=“Montbrilland”});
}
公共类ServiceSectionModel
{
公共字符串ID{get;set;}
公共字符串名称{get;set;}
公共SolidColorBrush颜色{get;set;}
}
ObservableCollection服务=新的ObservableCollection();
}
}

步骤2:XAML代码:

    <ComboBox x:Name="ComboBox1" HorizontalAlignment="Center" VerticalAlignment="Center">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Background="{Binding Color}" Text="{Binding ID}" Margin="0,0,10,0" Padding="5,2,10,2"></TextBlock>
                    <TextBlock Text="{Binding Name}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>


您可以通过使用数据模板来实现这一点,如果您向我展示C代码,我可以编写一个示例。当然,我会编辑。谢谢你。看看这个问题。我真的不明白他是如何定义组合框中的“网格”的。我必须创建三个DataTemplates,每个集合和显示一个?我可以写一个如何使用DataTemplates的示例,但我必须承认它对我来说有点过于复杂,可观察的集合已经包含INotifyPropertyChanged接口,是否有理由使用RaisePropertyChanged事件?好的,已经谢谢你的帮助了。我理解你的代码,但我不知道如何将它合并到我的DataTrigger中。我必须为每个集合制作一个DataTemplate,对吗?不一定,例如,你可以添加更多的文本块或其他控件,比如内部的另一个组合框,thin
        <ComboBox Name="CbService" HorizontalAlignment="Left" Margin="115,67,0,0" VerticalAlignment="Top" Width="150" ItemsSource="{Binding}" SelectionChanged="CbRecherche_SelectionChanged" KeyboardNavigation.TabIndex="1" >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Libelle}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid x:Name="gd" TextElement.Foreground="Black" Background="White">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition  Width="50"/>
                                    <ColumnDefinition Width="Auto" MinWidth="50" />
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Rectangle Fill="{Binding Fond}" Grid.Column="0"/>
                                <TextBlock Margin="5" Grid.Column="0" Text="{Binding ID}"/>
                                <TextBlock Margin="5" Grid.Column="1" Text="{Binding Libelle}"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>
namespace WpfApp1
{

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        ComboBox1.ItemsSource = Services;
        Services.Add(new ServiceSectionModel { Color = Brushes.Red, ID = "Clem", Name = "Clementine" });
        Services.Add(new ServiceSectionModel { Color = Brushes.White, ID = "011", Name = "Logistique" });
        Services.Add(new ServiceSectionModel { Color = Brushes.Green, ID = "MBT", Name = "Montbrilland" });
    }

    public class ServiceSectionModel
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public SolidColorBrush Color { get; set; }
    }

    ObservableCollection<ServiceSectionModel> Services = new ObservableCollection<ServiceSectionModel>();
}
    <ComboBox x:Name="ComboBox1" HorizontalAlignment="Center" VerticalAlignment="Center">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Background="{Binding Color}" Text="{Binding ID}" Margin="0,0,10,0" Padding="5,2,10,2"></TextBlock>
                    <TextBlock Text="{Binding Name}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>