简体   繁体   English

如何在不使用集合索引的情况下绑定到集合?

[英]How to bind to a collection without using an index of the collection?

I am trying to learn the MVVM pattern in WPF and I am running into some issues with bindings and collections. 我正在尝试学习WPF中的MVVM模式,并且遇到绑定和集合的一些问题。 I don't like binding to the index of the collection because the collection changes. 我不喜欢绑定到集合的索引,因为集合发生了变化。

I have MyModelClass : ObservableCollection<MyData> 我有MyModelClass : ObservableCollection<MyData>

The MyData class has a property called DataValue in it to hold my custom information. MyData类具有一个名为DataValue的属性,用于保存我的自定义信息。 I fill the MyModelClass with MyData objects. 我用MyData对象填充MyModelClass In the view model class ( MyViewModel ) I have a property CurrentData of type MyModelClass . 在视图模型类( MyViewModel )中,我具有类型为MyModelClass的属性CurrentData

Now in this case, my binding is to a combo box. 现在,在这种情况下,我的绑定是组合框。 Here is what the binding looks like: 绑定看起来像这样:

<ComboBox Name="cmbxData" 
  ItemsSource="{Binding Path=CurrentData}"
  DisplayMemberPath="DataValue"
  SelectedValuePath="DataValue"
  SelectedValue="{Binding Path=CurrentData[0].DataValue}"/>

This all works fine and the MyViewModel class has the interface setup for PropertyChanged notifications. 所有这些都可以正常工作,并且MyViewModel类具有PropertyChanged通知的接口设置。 I need to refresh the information this holds every few seconds. 我需要每隔几秒钟刷新一次保存的信息。

I am refreshing the collection by doing something like this inside the MyModelClass : 我通过在MyModelClass执行以下操作来刷新集合:

public Class MyModelClass : ObservableCollection<MyData>
{
    private static MyModelClass current = null;
    public static MyModelClass Current
    {
        get
        {
             if(current == null)
                 current = new MyModelClass();

             return current;
        }
    }

    private void UpdateModel(MyData newData)
    {
         MyModelClass newModel = new MyModelClass();
         newModel.Add(newData);
         current = newModel;

    }
}

Inside MyViewModel there is this: MyViewModel里面是这样的:

MyModelClass CurrentData = MyModelClass.Current;

I tried my best to give as much code as I could and keep it simple. 我尽力提供尽可能多的代码并保持简单。 Please let me know if there is more needed to clarify. 请让我知道是否还有更多需要澄清的地方。 Thank you for any suggestions on how to fix my issue. 感谢您对如何解决我的问题的任何建议。

In case you missed it: Problem: How can I bind to the collection, or the value I want to display in the collection, without using the index like I have shown above ( SelectedValue="{Binding Path=CurrentData[0].DataValue}" )? 如果您错过了它:问题:如何绑定到集合或要在集合中显示的值,而无需使用上面显示的索引( SelectedValue="{Binding Path=CurrentData[0].DataValue}" )? How can I fix this or change my implementation to be correct? 如何解决此问题或更改我的实现以使其正确?

Thanks, 谢谢,

Add a property on MyModelClass called "SelectedData" or something similiar. 在MyModelClass上添加一个名为“ SelectedData”的属性或类似的名称。 Then bind that property to the SelectedItem property of ComboBox 然后将该属性绑定到ComboBox的SelectedItem属性

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

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