如何在ModelView中设置ComboBox选定项?

时间:2016-05-04 12:30:04

标签: c# wpf combobox mvvm-light

我有静态组合框项目。我的XAML看起来像这样:

    <ComboBox x:Name="comboBox" SelectedItem="{Binding MySelectedItem}" HorizontalAlignment="Left" Margin="58,7,0,0" VerticalAlignment="Top" Width="120">
        <ComboBoxItem Name="cbi1">Item 1</ComboBoxItem>
        <ComboBoxItem Name="cbi2">Item 2</ComboBoxItem>
        <ComboBoxItem Name="cbi3">Item 3</ComboBoxItem>
    </ComboBox>

模型视图:

    // How to set initial value here or in constructor?
    private ComboBoxItem _mySelectedItem;
    public ComboBoxItem MySelectedItem
    {
        get
        {
            return _mySelectedItem;
        }
        set
        {
            Console.WriteLine("Value = {0}", (value as ComboBoxItem).Name);
        }
    }

如何设置具有Name的组合框的选定项?在从持久存储中读取之后,我想在MV构造函数中设置所选项。

2 个答案:

答案 0 :(得分:2)

您需要添加的是组合框中的SelectedValuePath =“Name”。然后在您的ViewModel构造函数中,像这样添加您的启动项。我将我的SelectedDropDown属性设置为一个字符串,不确定你是否必须将它作为ComboBoxItem。

 public MainFormViewModel()
    {
        SelectedDropDown = "cbi1";
    }
 private string _SelectedDropDown;
    public string SelectedDropDown
    {
        get { return _SelectedDropDown; }
        set { _SelectedDropDown = value; NotifyPropertyChanged("SelectedDropDown"); }
    }

这是ComboBox代码,测试它,它工作正常。

   <ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="114,213,0,0" SelectedValuePath="Name"  SelectedValue="{Binding SelectedDropDown }"  VerticalAlignment="Top" Width="120" Grid.Column="1">
            <ComboBoxItem Name="cbi1">Item 1</ComboBoxItem>
            <ComboBoxItem Name="cbi2">Item 2</ComboBoxItem>
            <ComboBoxItem Name="cbi3">Item 3</ComboBoxItem>
        </ComboBox>

答案 1 :(得分:0)

您必须从ViewModel创建集合才能使用SelectedItem

* .xaml(查看)

<ComboBox x:Name="comboBox" SelectedValue="{Binding MySelectedItem, Mode=TwoWay}" ItemsSource="{Binding MyCollection}" Height="25"/>

*。cs(ViewModel)

public class MainViewModel : INotifyPropertyChanged
{
    public MainViewModel()
    {
        for (int i = 0; i < 10; i++)
        {
            MyCollection.Add("Item " + i);
        }
        MySelectedItem = "Item 2";
    }


    private ObservableCollection<string> myCollection = new ObservableCollection<string>();

    public ObservableCollection<string> MyCollection
    {
        get
        {
            return myCollection;
        }
        set
        {
            myCollection = value;
            NotifyPropertyChanged("MyCollection");
        }
    }


    private string _mySelectedItem;
    public string MySelectedItem
    {
        get
        {
            return _mySelectedItem;
        }
        set
        {
            _mySelectedItem = value;
            NotifyPropertyChanged("MySelectedItem");
        }
    }

    //NotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}