WPF正在将SelectedItem绑定到另一个对象的属性



我想做的是查找一个"类别";根据CurrentItem.CategoryId将SelectedItem绑定到,其中CurrentItem.CatagoryId与Category.Id.匹配

<ListBox ItemsSource="{Binding Categories}" SelectedItem="{Binding CurrentItem.CategoryId, Mode=TwoWay>

我想过使用IValueConverter,但我不确定如何将Category.Id作为ConverterParameter传递。或者如果这是正确的方法。我不得不想象这是一个常见的用例,但我甚至不知道该在谷歌上搜索什么。欢迎提出任何建议!

编辑:好的,更多的细节与完整的代码。交易具有称为";CategoryId";,其匹配"0";Id";属性。我的最终目标是使列表框中的selectedItem成为DataGrid中当前SelectedTransaction所指的任何类别。

它应该是双向绑定,ListView应该初始化为Transaction.CategoryId指向的Category,如果ListView更新了,那么Transaction.CateCategoryId属性也应该更新。希望这是清楚的。

TransactionWindow.xaml

<Window x:Class="Budgeter.TransactionsWindow"
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:Budgeter"
mc:Ignorable="d"
Title="TransactionsWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10px" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="10px" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10px" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="10px" />
</Grid.RowDefinitions>
<DataGrid x:Name="grid" ItemsSource="{Binding Transactions}" AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionMode="Single" Grid.Row="1" Grid.Column="1" Grid.RowSpan="4" SelectedItem="{Binding SelectedTransaction}" SelectionChanged="grid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Binding="{Binding Date}" Width="*" IsReadOnly="True" />
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="*" MinWidth="200" IsReadOnly="True" />
<DataGridTextColumn Header="Amount" Binding="{Binding Amount}" Width="*" IsReadOnly="True" />
<DataGridTextColumn Header="Category" Binding="{Binding Category.Description}" Width="*" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
<DockPanel Grid.Row="1" Grid.Column="2" Grid.RowSpan="3" Margin="5">
<TextBlock Text="Category:" DockPanel.Dock="Top" />
<ListBox ItemsSource="{Binding Categories}" SelectedItem="{Binding ???" DockPanel.Dock="Bottom">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</Grid>
</Window>

TransactionWindow.xaml.cs

public partial class TransactionsWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public IList<CategoryViewModel> Categories { get; }
public IList<TransactionViewModel> Transactions { get; }
public TransactionViewModel SelectedTransaction { get; set; }
public TransactionsWindow(IList<CategoryViewModel> categories, IList<TransactionViewModel> transactions)
{
InitializeComponent();
this.Categories = categories;
this.Transactions = transactions;
if (this.Transactions != null && this.Transactions.Count > 0)
{
this.SelectedTransaction = transactions[0];
}
this.DataContext = this;

}
private void grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.OnPropertyChanged(nameof(SelectedTransaction));
}
protected void OnPropertyChanged(string name = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}

这应该有效:

<ListBox ItemsSource="{Binding Categories}"
SelectedValuePath="Id"
SelectedValue="{Binding SelectedTransaction.CategoryId"}>

请注意,SelectedIndex、SelectedItem和SelectedValue默认绑定TwoWay。


SelectedTransaction属性应触发PropertyChanged事件:

private TransactionViewModel selectedTransaction;
public TransactionViewModel SelectedTransaction
{
get { return selectedTransaction; }
set
{
selectedTransaction = value;
OnPropertyChanged(nameof(SelectedTransaction));
}
}

您还应该考虑将所有TransactionsWindow属性移动到另一个类中,并将其调用,例如MainViewModel:

DataContext = new MainViewModel();

相关内容

  • 没有找到相关文章

最新更新