简体   繁体   中英

C# WPF Binding between combobox and listbox

I have a problem with binding between combobox and listbox. I have four classes Account1, Account2, Account3, Client. So class Client has a 3 lists with accounts. In class client are 3 lists: List<Account1> , List<Account2> , List<Account3> .

In xaml MainWindow, I have a combobox with client's surnames and list box with client's accounts and my problem is that if I choose in combobox client's surname in list box should display lists with accounts for this client. I don't know how I can do this.

Sorry for my English but it isn't my mother's language.

xaml

<Window x:Class="kontowe.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:kontowe"
    mc:Ignorable="d"
    Title="MainWindow" Height="400" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="71*"/>
        <RowDefinition Height="203*"/>
        <RowDefinition Height="95*"/>
    </Grid.RowDefinitions>
    <Label Content="Wybierz klienta" HorizontalAlignment="Left" Height="28" Margin="10,20,0,0" VerticalAlignment="Top" Width="122"/>
    <ComboBox x:Name="klienci" HorizontalAlignment="Left" Height="28" Margin="162,20,0,0" VerticalAlignment="Top" Width="191"
              DataContext="{Binding ElementName=lbStudenci, Path=SelectedItem}"/>
    <Button x:Name="nowyklient" Content="Dodaj" HorizontalAlignment="Left" Margin="406,23,0,0" VerticalAlignment="Top" Width="75"/>
    <GroupBox x:Name="gbListakont" Header="Konta klienta" HorizontalAlignment="Left" Height="174" Margin="17,19,0,0" Grid.Row="1" VerticalAlignment="Top" Width="471">
        <Grid x:Name="gridklienta" HorizontalAlignment="Left" Height="134" Margin="10,10,0,0" VerticalAlignment="Top" Width="432">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="317*"/>
                <ColumnDefinition Width="115*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="nowekonto" Content="Dodaj konto" Grid.Column="1" HorizontalAlignment="Left" Height="21" Margin="14,15,0,0" VerticalAlignment="Top" Width="91"/>
            <Button x:Name="usuwaniekonta" Content="Usuń konto" Grid.Column="1" HorizontalAlignment="Left" Height="21" Margin="14,57,0,0" VerticalAlignment="Top" Width="91"/>
            <TextBox x:Name="listakont" HorizontalAlignment="Left" Height="124" Margin="20,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="279"
                     Text="{Binding Path=jakiekonta, UpdateSourceTrigger=PropertyChanged}"/>
        </Grid>
    </GroupBox>
    <GroupBox x:Name="obsluga" Header="Operacje konta" HorizontalAlignment="Left" Height="75" Margin="25,10,0,0" Grid.Row="2" VerticalAlignment="Top" Width="473">
        <Grid x:Name="gridkonta" HorizontalAlignment="Left" Height="38" Margin="4,2,0,0" VerticalAlignment="Top" Width="453">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="206*"/>
                <ColumnDefinition Width="247*"/>
            </Grid.ColumnDefinitions>
            <TextBox HorizontalAlignment="Left" Height="28" Margin="10,10,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="186"/>
            <Button x:Name="wplata" Content="Wpłać" Grid.Column="1" HorizontalAlignment="Left" Height="28" Margin="19,10,0,0" VerticalAlignment="Top" Width="92"/>
            <Button x:Name="wyplata" Content="Wypłać" Grid.Column="1" HorizontalAlignment="Left" Height="28" Margin="148,10,0,0" VerticalAlignment="Top" Width="89"/>
        </Grid>
    </GroupBox>

</Grid>

class Client

public class Klient 
{
    private static int KlientID = 1;
    public string numer { get; set; }

    public string numerklienta { get; set; }
    public string nazwisko { get; set; }

    public List<Biezace> rory = new List<Biezace>();
    public List<Lokata> lokaty = new List<Lokata>();
    public List<Debet> debety = new List<Debet>();

    public Klient(string nazwisko)
    {
        this.numerklienta = (KlientID++).ToString();
        this.nazwisko = nazwisko;

        rory.Add(new Biezace());
        lokaty.Add(new Lokata());
        debety.Add(new Debet());
        jakieKonta.Add(new ListaKont());
    }

    public Klient(string nazwisko, string nur)
    {
        this.numerklienta = nur;
        this.nazwisko = nazwisko;

        rory.Add(new Biezace());
        lokaty.Add(new Lokata());
        debety.Add(new Debet());
        jakieKonta.Add(new ListaKont());
    }




    public override string ToString()
    {
        //return nazwisko + " " + imie + " [" + sr + "]";
        string s = this.nazwisko + " ";// + String.Format("{0:N2}", srednia());

        return s;
    }
}

Simplified answer so the general structure is clear:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <ComboBox Grid.Column="0" 
              x:Name="ClientsComboBox" 
              ItemsSource="{Binding Clients}"/>
    <ListBox Grid.Column="1" 
             ItemsSource="{Binding ElementName=ClientsComboBox, Path=SelectedItem.rory}"/>
    <ListBox Grid.Column="2"
             ItemsSource="{Binding ElementName=ClientsComboBox, Path=SelectedItem.lokaty}"/>
    <ListBox Grid.Column="3"
             ItemsSource="{Binding ElementName=ClientsComboBox, Path=SelectedItem.debety}"/>
</Grid>

This way the list boxes bind to the selected item in the combo box.

Note that if the combo and list boxes should automatically update when items are added or removed from the lists, the lists should be replaced with ObservableCollections like this:

public class Klient 
{
    private static int KlientID = 1;
    public string numer { get; set; }

    public string numerklienta { get; set; }
    public string nazwisko { get; set; }

    public ObservableCollection<Biezace> rory = new ObservableCollection<Biezace>();
    public ObservableCollection<Lokata> lokaty = new ObservableCollection<Lokata>();
    public ObservableCollection<Debet> debety = new ObservableCollection<Debet>();

If properties of Klient and the account classes can also change these classes should also implement INotifyPropertyChanged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM