我在组合框和列表框之间存在绑定问题。我有四个班级Account1,Account2,Account3,Client。所以类客户端有3个帐户列表。
在课堂上,客户有3个列表:List<Account1>
,List<Account2>
,List<Account3>
。
在xaml MainWindow中,我有一个带有客户姓氏的组合框和带有客户帐户的列表框,我的问题是如果我选择组合框客户端的姓氏列表框中应该显示包含此客户帐户的列表。我不知道怎么做到这一点。
对不起我的英语,但这不是我母亲的语言。
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;
}
}
答案 0 :(得分:0)
简化回答,所以一般结构很清楚:
<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>
这样,列表框就会绑定到组合框中的选定项目。
请注意,如果在列表中添加或删除项目时组合框和列表框应自动更新,则列表应替换为ObservableCollections,如下所示:
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>();
如果Klient的属性和帐户类也可以更改这些类,也应该实现INotifyPropertyChanged。