繁体   English   中英

将ComboBox TwoWay绑定到属性

[英]Binding ComboBox TwoWay to Property

我正在尝试使用双向绑定。

这些组合框应将它们自己(TwoWay)绑定到VM中的属性:

<ComboBox x:Name="_cmbCatT1" Margin="5,1,5,10" 
          ItemsSource="{Binding MVM.CategoryLinkCollection}"
          DisplayMemberPath="Category.Name" 
          SelectedValue="{Binding MVM.SelectedTier1, Mode=TwoWay}" 
          SelectedValuePath="Category"/>
<ComboBox x:Name="_cmbCatT2" Margin="5,1,5,10" 
          DataContext="{Binding SelectedItem, ElementName=_cmbCatT1}" 
          ItemsSource="{Binding CategoryLinkCollection}" 
          DisplayMemberPath="Category.Name" 
          SelectedValue="{Binding MVM.SelectedTier2, ElementName=_vManipulation, Mode=TwoWay}" 
          SelectedValuePath="Category"/>
<ComboBox x:Name="_cmbCatT3" Margin="5,1,5,10" 
          DataContext="{Binding SelectedItem, ElementName=_cmbCatT2}" 
          ItemsSource="{Binding CategoryLinkCollection}" 
          DisplayMemberPath="Category.Name" 
          SelectedValue="{Binding MVM.SelectedTier3, ElementName=_vManipulation, Mode=TwoWay}" 
          SelectedValuePath="Category"/>

VM:

private string selectedName;
public string SelectedName {
    get {
        return this.selectedName;
    }
    set {
        this.selectedName = value;
        OnPropertyChanged("SelectedName");
    }
}
private string selectedDescription;
public string SelectedDescription {
    get {
        return this.selectedDescription;
    }
    set {
        this.selectedDescription = value;
        OnPropertyChanged("SelectedDescription");
    }
}
private string selectedRemark;
public string SelectedRemark {
    get {
        return this.selectedRemark;
    }
    set {
        this.selectedRemark = value;
        OnPropertyChanged("SelectedRemark");
    }
}
private string selectedValue; 
public string SelectedValue {
    get {
        return this.selectedValue;
    }
    set {
        this.selectedValue = value;
        OnPropertyChanged("SelectedValue");
    }
}
private Model.Room selectedRoom;
public Model.Room SelectedRoom {
    get {
        return this.selectedRoom;
    }
    set {
        this.selectedRoom = value;
        OnPropertyChanged("SelectedRoom");
    }
}
private Model.Locker selectedLocker;
public Model.Locker SelectedLocker {
    get {
        return this.selectedLocker;
    }
    set {
        this.selectedLocker = value;
        OnPropertyChanged("SelectedLocker");
    }
}
private Model.Category selectedTier1;
public Model.Category SelectedTier1 {
    get {
        return this.selectedTier1;
    }
    set {
        this.selectedTier1 = value;
        OnPropertyChanged("SelectedTier1");
    }
}
private Model.Category selectedTier2;
public Model.Category SelectedTier2 {
    get {
        return this.selectedTier2;
    }
    set {
        this.selectedTier2 = value;
        OnPropertyChanged("SelectedTier2");
    }
}
private Model.Category selectedTier3;
public Model.Category SelectedTier3 {
    get {
        return this.selectedTier3;
    }
    set {
        this.selectedTier3 = value;
        OnPropertyChanged("SelectedTier3");
    }
}
private Model.Manufacturer selectedManufacturer;
public Model.Manufacturer SelectedManufacturer {
    get {
        return this.selectedManufacturer;
    }
    set {
        this.selectedManufacturer = value;
        OnPropertyChanged("SelectedManufacturer");
    }
}

请注意,ItemsSource的数据类型是“ CategoryLinkCollection”,其中包含“ Category”-属性和Obs.Collection“ CategoryLinkCollection”。 通过SelectedValuePath,“类别”在VM的属性中“保存”。

只要我在ComboBox中选择一个项目,一切就可以正常工作,但是当我手动设置VM属性时,ComboBox不会从包含定义类别的ItemsSource中预选该项目。 普通的字符串值只能用于finde(文本框)

我认为由于类型不同(ComboBox = CategoryLinkCollection,ValuePath到Category,Property = Category),它将永远无法工作,但也许有些人可以证明我错了。 如果您需要更多信息,请告诉我。

更新1:

我只是记得主要的原因应该在其他地方,因为“制造商”模型没有包装器-因此ItemsSource包含与属性相同的数据类型的Collection。

XAML:

<ComboBox x:Name="_cmbManufacturer" 
          ItemsSource="{Binding MVM.ManufacturerCollection}" 
          DisplayMemberPath="Name" 
          SelectedItem="{Binding MVM.SelectedManufacturer, Mode=TwoWay}"/>

VM:见上文。

更新2:

首先,请远离那些糟糕的类别,而转到Update 1中提到的制造商,因为这是相同的问题,但是更容易进入。

我现在在手动设置之后检查了SelectedManufacturer-Property,以防万一设置时出现问题。 SelectedManufacturer-Property包含制造商(不为null),但UI仍未更新。

更新3:

使用诊断NS之后,我从“输出窗口”中获得了以下输出:

System.Windows.Data警告:60:BindingExpression(哈希= 43478430):默认模式解析为TwoWay

System.Windows.Data警告:61:BindingExpression(哈希= 43478430):默认更新触发器已解析为PropertyChanged

System.Windows.Data警告:62:BindingExpression(哈希= 43478430):附加到System.Windows.Controls.ComboBox.SelectedItem(哈希= 10372298)

System.Windows.Data警告:67:BindingExpression(hash = 43478430):解析源

System.Windows.Data警告:70:BindingExpression(哈希= 43478430):找到数据上下文元素:ComboBox(哈希= 10372298)(确定)

System.Windows.Data警告:78:BindingExpression(哈希= 43478430):激活与根项操纵(哈希= 64100268)

System.Windows.Data警告:107:BindingExpression(hash = 43478430):在级别0使用缓存的访问器进行操作。MVM:RuntimePropertyInfo(MVM)

System.Windows.Data警告:104:BindingExpression(hash = 43478430):使用访问器RuntimePropertyInfo(MVM)将级别0的项替换为Manipulation(hash = 64100268)

System.Windows.Data警告:101:BindingExpression(hash = 43478430):使用RuntimePropertyInfo(MVM)从操作(hash = 64100268)在级别0上的GetValue:ManipulationViewModel(hash = 11088040)

System.Windows.Data警告:108:BindingExpression(哈希= 43478430):在级别1-对于ManipulationViewModel.SelectedManufacturer找到访问器RuntimePropertyInfo(SelectedManufacturer)

System.Windows.Data警告:104:BindingExpression(hash = 43478430):使用访问器RuntimePropertyInfo(SelectedManufacturer)用ManipulationViewModel(hash = 11088040)替换级别1的项

System.Windows.Data警告:101:BindingExpression(hash = 43478430):使用RuntimePropertyInfo(SelectedManufacturer)从ManipulationViewModel(hash = 11088040)处于级别1的GetValue:制造商(hash = 14500136)

System.Windows.Data警告:80:BindingExpression(哈希= 43478430):TransferValue-获取原始值制造商(哈希= 14500136)

System.Windows.Data警告:84:BindingExpression(哈希= 43478430):TransferValue-隐式转换器产生的制造商(哈希= 14500136)

System.Windows.Data警告:89:BindingExpression(哈希= 43478430):TransferValue-使用最终值制造商(哈希= 14500136)

更新4:

制造商类别:

public class Manufacturer : Base.SqlBase {
    public Manufacturer(int id, string name) {
        this.SqlID = id;
        this.Name = name;
    }
}

SqlBase类:

public abstract class SqlBase : INotifyPropertyChanged {
    public int SqlID { get; set; }
    private string _name;
    public string Name {
        get {
            return _name;
        }
        protected set {
            this._name = value;
            PropertyChangedHandler("Name");
        }
    }

    public void SetId(int id) {
        this.SqlID = id;
        PropertyChangedHandler("SqlID");
    }

    private void PropertyChangedHandler(string propertyName) {
        PropertyChangedEventHandler temp = PropertyChanged;
        if (temp != null) {
            temp(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

实体类别:

public class Entity : Base.SqlBase {

    public string Description { get; private set; }
    public string Remark { get; private set; }
    public int Value { get; private set; }
    public Model.CategoryWrapper Categories { get; private set; }
    public Model.Manufacturer Manufacturer { get; private set; }
    public Model.Locker Locker { get; private set; }

    public Entity(string name, string desc, string remark, int value, Model.CategoryWrapper cat, Model.Manufacturer manuf, Model.Locker locker) {           
        this.Name = name;
        this.Description = desc;
        this.Remark = remark;
        this.Value = value;
        this.Categories = cat;
        this.Manufacturer = manuf;
        this.Locker = locker;
    }

    public Entity(int id, string name, string desc, string remark, int value, Model.CategoryWrapper cat, Model.Manufacturer manuf, Model.Locker locker) {
        this.SqlID = id;
        this.Name = name;
        this.Description = desc;
        this.Remark = remark;
        this.Value = value;
        this.Categories = cat;
        this.Manufacturer = manuf;
        this.Locker = locker;
    }
}

我得到GridView(类型:实体)的SelectedItem,并将其交给实例化ViewModel的Manipulation-View。 在此ViewModel中,将实体划分为各个部分(例如,制造商),并设置属性(例如,SelectedManufacturer)。 所有这些步骤都构建View 之前 (在Initializecomponent之前)执行。 我认为以这种方式,视图必须在初始化时获取选定的值-还是我错了?

更新5:

我真的不知道为什么,但是在窗口初始化时设置制造商时,制造商对象的PropertyChanged-Property为null-当我通过选择一项设置制造商时,属性不为null。

在绑定中使用更新源触发器属性,并将其设置为PropertyChanged:

<ComboBox x:Name="_cmbManufacturer" ItemsSource="{Binding MVM.ManufacturerCollection}" DisplayMemberPath="Name" SelectedItem="{Binding MVM.SelectedManufacturer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Howsit Chill-X,我是否正确理解核心问题是更新视图模型时希望组合框反映更改后的选择? 如果是这样,这是您如何做到的精简版本。

重要注意事项(将所选项目绑定到另一个名为SelectedCategory的属性。当您在VM中更改SelectedCategory时,UI将会更改,并且如果您在组合框中选择新项目,则SelectedCategory属性将被更新。

这是视图模型:

public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Category> _categoryLinkCollection;
    public ObservableCollection<Category> CategoryLinkCollection
    {
        get
        {
            return this._categoryLinkCollection;
        }
        set
        {
            if (value != this._categoryLinkCollection)
            {
                this._categoryLinkCollection = value;
                OnPropertyChanged("CategoryLinkCollection");
            }
        }
    }

    private Category _selectedCategory;
    public Category SelectedCategory
    {
        get
        {
            return this._selectedCategory;
        }
        set
        {
            this._selectedCategory = value;
            OnPropertyChanged("SelectedCategory");
        }
    }
}

这是xaml:

<ComboBox ItemsSource="{Binding CategoryLinkCollection, Mode=TwoWay}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedCategory, Mode=TwoWay}" />

我只是使用了页面后面的代码将虚拟数据添加到集合中(在window_loaded上,单击按钮以显示可以更新VM属性和UI更新。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // Set the View's DataContext to our ViewModel
        var vm = new ViewModel();
        this.DataContext = new ViewModel();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // Populate Category Collection with dummy data.
        var vm = ((ViewModel)this.DataContext);
        vm.CategoryLinkCollection = new ObservableCollection<Category>()
        {
            new Category("Cat 1"),
            new Category("Cat 2"),
            new Category("Cat 3"),
            new Category("Cat 4"),
            new Category("Cat 5"),
            new Category("Cat 6"),
        };
        vm.SelectedCategory = vm.CategoryLinkCollection[0];
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        var vm = ((ViewModel)this.DataContext);
        vm.SelectedCategory = vm.CategoryLinkCollection[3];
    }
}

现在,您有了一个从VM和UI更新的ComboBox。

似乎我已经解决了这个难题。

说明:

应该预先选择实体给定的ComboBox-Value的“ Manipulation”-视图将接收它自己的ObservableCollection实例-但是Manufacturer-Object被传递给该表单-因此它不包含在Collection中。

解:

重写Equals-Method来检查SQLID属性,而不是通过默认方法来确定相等性可以解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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