简体   繁体   English

如何使用MVVM使用访问填充组合框

[英]How to populate a combobox with access using MVVM

I'm new to C# and I'm trying to create a code using MVVM pattern, but I don't know how to populate a combobox using that pattern. 我是C#的新手,正在尝试使用MVVM模式创建代码,但是我不知道如何使用该模式填充组合框。 Please Give me help to create the ViewModel and the binding to the xaml. 请帮助我创建ViewModel并绑定到xaml。

Code Model: 代码模型:

 public int Cd_Raca
    {
        get;
        set
        {
            if(Cd_Raca != value)
            {
                Cd_Raca = value;
                    RaisePropertyChanged("Cd_Raca");
            }
        }
    }

    public string Nm_Raca
    {
        get;
        set
        {
            if(Nm_Raca != value)
            {
                Nm_Raca = value;
                    RaisePropertyChanged("Nm_Raca");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string property)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

Xaml: Xaml:

<ComboBox x:Name="dsCmbRaca" HorizontalAlignment="Left" Margin="438,4,0,0"
                             VerticalAlignment="Top" Width="94" Height="19"/>

Use the ItemsSource Property and set it to an enumeration of objects. 使用ItemsSource属性并将其设置为对象的枚举。 With DisplayMemberPath you can set it to a property of a single object of your list if the list is not just a list of strings. 如果列表不仅仅是字符串列表,则可以使用DisplayMemberPath将其设置为列表的单个对象的属性。
Ie in my sample the object of the list has a Description property for display and a Value property for the selected value. 即在我的示例中,列表的对象具有用于显示的Description属性和用于所选值的Value属性。

All bindings in the sample need to be a property in your ViewModel (=DataContext). 样本中的所有绑定都必须是ViewModel(= DataContext)中的一个属性。

<ComboBox DisplayMemberPath="Description" HorizontalAlignment="Left" 
                  VerticalAlignment="Top" Width="120"
                  ItemsSource="{Binding myList}"
                  SelectedValue="{Binding mySelectedValue}" SelectedValuePath="Value" />  

Edit: 编辑:
The List property could look like this: List属性如下所示:

    public IList<MyObject> myList { get { return new List<MyObject>();} }

The Object could look like this for example: 该对象可能看起来像这样:

public class MyObject
{
    public string Description { get; }
    public enum Value { get;}
}

The Object is optional. 对象是可选的。 You could just pass a list of strings. 您可以只传递一个字符串列表。

Disclaimer: I hacked this in notepad. 免责声明:我在记事本中修改了此内容。 I hope it compiles. 我希望它可以编译。

UPDATE 更新
Looking at your code at least from what you post your properties are not implemented correctly. 至少从您发布的内容来看您的代码没有正确实现。 You need a backing field if you code it like you have: 如果像这样进行编码,则需要一个备用字段:

private int _cd_Raca;
private string _nm_Raca;

public int Cd_Raca
    {
        get{ return _cd_Raca;}
        set
        {
            if(_cd_Raca != value)
            {
                _cd_Raca = value;
                    RaisePropertyChanged("Cd_Raca");
            }
        }
    }

    public string Nm_Raca
    {
        get{return _nm_Raca;}
        set
        {
            if(_nm_Raca != value)
            {
                _nm_Raca = value;
                    RaisePropertyChanged("Nm_Raca");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string property)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

Reading your comment to my first answer seems you might have a specific use case. 阅读对我的第一个答案的评论似乎可以使用特定的用例。 So if this update does not help maybe you can add some more information to your question. 因此,如果此更新无济于事,您可以在问题中添加更多信息。

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

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