简体   繁体   English

绑定列表 <string> 到组合框以在视图中显示(MVVM)

[英]Binding a List<string> to a ComboBox for display in a view (MVVM)

I am attempting to bind a list to a combobox. 我正在尝试将列表绑定到组合框。 I want to display this list of options within the Combobox itself. 我想在组合框本身中显示此选项列表。 (Later to allow the user to select an item 'SelectedItem', I'll cross that bridge when I get there) (以后允许用户选择一个项目“ SelectedItem”,到达那儿我会过桥)

MyCode.cs MyCode.cs

// List of values for 'Type' dropdown
    private static readonly List<string> MarkerTypeList = new List<string>(new string[]
    {
        "Analog",
        "Digital"
    });

// Binding for viewing list in window
    public List<string> TypeOptions
    {
        get { return MarkerTypeList; }
    }

MyCode.xaml MyCode.xaml

        <ComboBox x:Name="myCombobox" HorizontalAlignment="Left" Margin="125,26,0,0" VerticalAlignment="Top" Width="70" Height="23" SelectedItem="" ItemsSource="{Binding TypeOptions}" />

The solution boiled down to changing this: 解决方案归结为更改此:

ItemsSource="{Binding TypeOptions}"

to this: 对此:

ItemsSource="{Binding Marker.TypeOptions}"

Thanks for the input, sorry you didnt have a whole lot to go on. 感谢您的输入,对不起,您没有很多事情要做。

If you bind you should have your INotifyPropertyChanged interface added to your class which is the actual ViewModel for your ComboBox. 如果绑定,则应将INotifyPropertyChanged接口添加到类中,该接口是ComboBox的实际ViewModel。 So - 1. Add the Interface to the class. 所以-1.将接口添加到类中。 2. Create RaisePropertyChanged function. 2.创建RaisePropertyChanged函数。 3. Call the function through the setter of the property. 3.通过属性的设置器调用函数。 This will push the updated value of the property through the binding and you will see the combobox populated. 这将通过绑定推送属性的更新值,您将看到已填充组合框。

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

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