Как добавить сетку в мой комбинированный список и отобразить данные obervableCollection? - PullRequest
1 голос
/ 14 октября 2019

Я использую комбинированный список, и я хотел бы действовать следующим образом: я выбираю элемент в cmb1, это позволяет отображать в cmb2 коллекцию.

Этот код позволяет мне получить нужные данные (результат 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, Имя, Годы.

Служба содержит цвет, идентификатор, имя.

Раздел содержит цвет, идентификатор, имя раздела.

Редактировать 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

В этом примере есть идентификатор, цвет только в части идентификатора и имя. Я не могу сделать это со своей строкой в ​​данный момент.

1 Ответ

1 голос
/ 14 октября 2019

Полагаю, вы сможете уменьшить размер ваших кодов, удалив событие 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...