【问题标题】:Combobox selected item from a different object来自不同对象的组合框选定项目
【发布时间】:2013-10-28 15:28:12
【问题描述】:

我有一个组合框,列表是使用 AccountType 类填充的,并且列表正在正确填充。

但是,当我将选定的 Item 属性绑定到作为类帐户的 Selected Account 时。在页面加载时,所选项目未更新。文本框等所有其他控件都在更新。

我们将不胜感激。

查看

ComboBox ItemsSource="{Binding AllAccountTypes}" DisplayMemberPath="AccountTypeName" 
  SelectedValuePath="AccountTypeName" SelectedItem="{Binding SelectedAccount}" />

AccountType 类

public class AccountType:IAccountType
{
    public string AccountTypeName { get; set; }
}

帐户类

public class Account: IAccount
{
    public int AccountNo { get; set; }
    public string AccountName { get; set; }
    public string AccountTypeName { get; set; }
    public int SubAccount { get; set; }
    public string Description { get; set; }
    public double Balance { get; set; }
    public string Note { get; set; }
    public bool Active { get; set; }

}

ViewModel 中的选定帐户

    public IAccount SelectedAccount { get { return selectedAccount; }
        set { selectedAccount = value;  }
    }

【问题讨论】:

    标签: c# xaml mvvm winrt-xaml


    【解决方案1】:

    首先,您的ViewModel 需要引发INotifyPropertyChangedPropertyChanged 事件。

    其次,你的绑定应该指定双向绑定:

    <ComboBox ItemsSource="{Binding AllAccountTypes}" DisplayMemberPath="AccountTypeName" 
      SelectedValuePath="AccountTypeName" SelectedItem="{Binding SelectedAccount, Mode=TwoWay}" />
    

    但第三个,我认为这里的主要问题是,您的组合框绑定到 AccountTypes 列表(即IAccountType),但您希望所选项目是IAccount。但是IAccountType 上没有IAccount 类型的属性。

    因此,您需要将 SelectedItem 绑定到 IAccountType 属性,或者将 SelectedValue 绑定到 ViewModel 上的字符串属性。例如:

    <ComboBox ItemsSource="{Binding AllAccountTypes}" DisplayMemberPath="AccountTypeName"   
     SelectedItem="{Binding SelectedAccountType, Mode=TwoWay}"  />
    

    并且在您的 ViewModel 中有一个要绑定的属性:

    public IAccountType SelectedAccountType
    {
        get { return selectedAccountType; }
        set
        {
            if (Equals(value, selectedAccountType)) return;
            selectedAccountType = value;
            OnPropertyChanged("SelectedAccountType");
        }
    }
    

    【讨论】:

    • 感谢 Mark 的及时回复,您对如何将我的 IAccount SelectedAccount.AccountTypeName 转换为 IAccountType 的看法?或者您能否发布一个关于如何将我的 IAccount SelectedAccount.AccountTypeName 转换为字符串的示例?我的模型实现错了吗?我应该使用而不是 public string AccountTypeName { get;放;在我的 IAccount 模型中,类似于 public IAccountType AccountTypeName { get;放; } 。谢谢
    • 你试过我的建议了吗?我会选择 IAccountType 类型的 SelectedAccountType 属性,然后在 SelectedItem 上绑定到它
    • 感谢马克的帮助,正如建议的那样,我创建了一个 IAccountType 属性并使用 SelectedType = AllAccountTypes.Where(x => x.AccountTypeName == SelectedAccount.AccountTypeName).FirstOrDefault() ;谢谢。
    【解决方案2】:

    这是因为您将 SelectedItem 绑定到 IAccount 对象,但您从下拉列表中选择了一个字符串。

    我会改为绑定到一个字符串,然后在 setter 中执行设置 SelectedAccount 属性所需的操作,如下所示:

        public string SelectedAccountName
        {
            get { return _selectedAccountName; }
            set
            {
                _selectedAccountName = value;
                SelectedAccount = AllAccounts.Where(x => x.AccountName == _selectedAccountName).First();
            }
        }
    

    使用这样的 XAML(我添加了高度和宽度值,因此下拉菜单不会很大):

    <ComboBox Height="20" Width="100" ItemsSource="{Binding AllAccountTypes}" DisplayMemberPath="AccountTypeName" SelectedValuePath="AccountTypeName" SelectedItem="{Binding SelectedAccountName}" />
    

    【讨论】:

    • 感谢 JMK 的及时回复。我尝试了您的代码,但不知何故它不起作用。只是想了解 setter 中的 lambda,我们为什么要更新 SelectedAccount? x.AccountName == _selectedAccountName 是什么意思?给您带来的不便深表歉意。我对 lambda 有点陌生。谢谢。
    猜你喜欢
    • 2012-05-15
    • 2015-09-30
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    相关资源
    最近更新 更多