【问题标题】:Databind in MVVM for loosely binded classesMVVM 中的数据绑定用于松散绑定的类
【发布时间】:2020-09-10 09:17:08
【问题描述】:

我是 MVVM 新手,遇到这样的情况:假设我有两个松散耦合的类:

public class Client
{
public string Id {get; set;}
public string Name {get; set;}
public string Adress {get; set;}
public string AccountId {get; set;}
}

public class Account
{
public string Id {get; set;}
public string BankName {get; set}
public string AccountDescription {get; set;}
}

其中 Client.AccountId == Account.Id

在我的 .xaml.cs 中有:

public partial class BankingView : UserControl, INotifyPropertyChanged
{
private ObservableCollection<Client> Clients {get; set;}
private GlobalAccounts Acounts {get; set;}
//geting data methods and other stuff
}

GlobalAccounts 只是一个单例帮助类,其中包含 ObservableCollection of Acounts 和一些其他帮助方法,以确保在需要时从 API 更新它,但为简单起见,我们只说它看起来像这样:

public class GlobalAccounts : INotifyPropertyChanged
{
    private ObservableCollection<Acount> Acounts {get; set;}
    //methods to update Acounts
}

在 xaml 中,我有一个简单的 ListBox,它应该显示所有客户和每个客户的 BankName/BankDescription:

                    <ListBox ItemsSource="{Binding Clients, ElementName=uc}" >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid VerticalAlignment="Center">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>

                                    <TextBlock Text="Name:" TextAlignment="Right"/>
                                    <TextBlock Grid.Column="1" Text="{Binding Name}"/>
                                    <TextBlock Grid.Row="1" Text="Adress:" TextAlignment="Right"/>
                                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Adress}"/>
                                    <TextBlock Grid.Row="2" Text="BankName:" TextAlignment="Right"/>
                                    <TextBlock Grid.Row="2" Grid.Column="1" Text={Binding ?????? BankName} Tooltip={Binding ?????? AccountDescription} />
                                                                            
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

限制:不能使用转换器。需要使用 ViewModel

在程序的其他地方,我需要从其他也具有 AccountId 属性的对象中获取 BankName/AccountDescription。

据我所知,ViewModel 应该如下所示:

public class ClientAccountsViewModel: INotifyPropertyChanged
{
    public string AccountId {get; set;} //Account Id that needs to be matched
    public GlobalAccounts Accounts {get; set;} //singletone of all the Accounts
    public Acount CorrespondingAccount {get; set;} //account coresponding to client
    //logic that would find the Corresponding account based on SelectedClient set/changed
}

问题:应该如何从我的 .xaml.cs/.xaml 添加和调用这种类型的 ViewModel 以及绑定应该是什么样子?

【问题讨论】:

  • 尚不清楚您要达到的目标。 BankingView 是否应该从所有客户的集合中选择一个客户?为什么需要了解帐户?视图模型模型应该知道客户和帐户之间的关系。
  • BankingView 假设显示所有客户并根据该客户的 AccountId 选择对应的帐户并相应地显示帐户信息。
  • 这听起来很奇怪。它应该只选择一个客户端,并且相应的帐户应该由视图模型选择。然后控件(或其他一些控件)可以显示所选帐户。
  • 不同的数据存储在不同的API中。客户和帐户只是相关,因为它们有关系:Client.AccountId == Account.Id。就是这样。
  • 当然,这是一个应该由视图模型管理的关系。

标签: c# wpf xaml mvvm


【解决方案1】:

问题在于,在这种情况下,view 模型类不应该是您所说的“松散耦合”。他们应该为视图建模,因此得名。

因此,如果您想显示Client 的帐户的BankName,您应该创建一个Client 的视图模型,该模型具有要绑定的BankNameAccount 属性。

换句话说;如果您当前正在绑定到某个域对象,您应该创建另一个类来包装该对象的属性并添加视图需要绑定到的任何属性。毕竟,这就是视图模型的用途。

帮助类或ListBox 的视图模型负责实例化这些“子”视图模型对象。

【讨论】:

    猜你喜欢
    • 2013-11-02
    • 2013-01-07
    • 1970-01-01
    • 2017-08-20
    • 2015-07-15
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多