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

时间:2019-10-14 08:56:09

标签: c# wpf mvvm

我使用了一个组合框,我想按如下步骤进行: 我在cmb1中选择一个元素,这允许在cmb2中显示一个Collection。

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

    <ComboBox Name="cmbResultatListe" 
              Margin="0,10,0,10"              
              Grid.Row="4"
              Grid.Column="2"
              Height="25"
              Width="250" >
        <ComboBox.Style>
            <Style TargetType="{x:Type ComboBox}">
                <Setter Property="ItemsSource" Value="{Binding Sections}"></Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SelectedChoiceList}" Value="Etablissement">
                        <Setter Property="ItemsSource" Value="{Binding Etablissements}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding SelectedChoiceList}" Value="Service">
                        <Setter Property="ItemsSource" Value="{Binding Services}"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Style>
    </ComboBox>

我现在想将组合框分成三个网格,这样我就可以进行以下操作:

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

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

我不知道该怎么办。

任何提示吗?

谢谢您的帮助。

编辑:

c#代码:

    private ObservableCollection<Etablissement> _EtablissementsUtilisateur;
    public ObservableCollection<Etablissement> EtablissementsUtilisateur
    {
        get
        {
            return _EtablissementsUtilisateur;
        }
        set
        {
            if (value != _EtablissementsUtilisateur)
            {
                _EtablissementsUtilisateur = value;
                RaisePropertyChanged(nameof(EtablissementsUtilisateur));
            }
        }
    }

    private ObservableCollection<ServiceSectionModel> _Services;
    public ObservableCollection<ServiceSectionModel> Services
    {
        get
        {
            return _Services;
        }
        set
        {
            if (value != _Services)
            {
                _Services = value;
                RaisePropertyChanged(nameof(Services));
            }
        }
    }

    private ObservableCollection<ServiceSectionModel> _Sections;
    public ObservableCollection<ServiceSectionModel> Sections
    {
        get
        {
            return _Sections;
        }
        set
        {
            if (value != _Sections)
            {
                _Sections = value;
                RaisePropertyChanged(nameof(Sections));
            }
        }
    }         
    private string _SelectedChoiceList;
    public string SelectedChoiceList
    {
        get
        {
            return _SelectedChoiceList;
        }
        set
        {
            if (value != _SelectedChoiceList)
            {
                _SelectedChoiceList = value;
                RaisePropertyChanged(nameof(SelectedChoiceList));
            }
        }
    }            

Etablissements = new ObservableCollection<Etablissement>((await _dataService.GetEtablissements().ConfigureAwait(false)));
Services = await _dataService.GetServicesAsync(false).ConfigureAwait(false);
Sections = await _dataService.GetSectionsAsync(_dataService.ParamGlobaux.IDEtablissement).ConfigureAwait(false);

Etablissement包含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>

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

Whatiwant

在此示例中,有一个ID,仅ID部分中的颜色和一个Name。我目前无法用我的琴弦来做。

1 个答案:

答案 0 :(得分:1)

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

第1步:C#代码:

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>();
}

}

第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>
相关问题