【问题标题】:How to set ComboBox selected item in ModelView?如何在 ModelView 中设置 ComboBox 选定项?
【发布时间】:2016-05-04 21:09:53
【问题描述】:

我有静态组合框项目。我的 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 构造函数中设置选定项。

【问题讨论】:

  • 你不能那样做。 ComboBoxItem 是一种引用类型,在您的 ViewModel 中您将无法获取 ComboBox 项的引用
  • 否则,您可以使用 SelectedIndex 代替 SelectedItem
  • SelectedIndex 有效,但这不是最好的解决方案。据我了解,要使 SelectedItem 工作,我必须从 ViewModel 创建和填充 ComboBox 项目?如果您知道如何,我们将不胜感激。
  • 您必须在 ViewModel 上创建它。我会回答的

标签: c# wpf combobox mvvm-light


【解决方案1】:

您需要在组合框中添加 SelectedValuePath="Name"。然后在您的 ViewModel 构造函数中,像这样添加您的启动项。我将我的 SelectedDropDown 属性设为字符串,但不确定在您的情况下是否必须将其作为 ComboBoxItem。

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

这是组合框代码,经过测试,它工作正常。

   <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>

【讨论】:

    【解决方案2】:

    您必须从 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));
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多