繁体   English   中英

WPF-当itemsource更改时,无法设置组合框selectedIndex

[英]WPF - Can't set combobox selectedIndex when itemssource changes

我有一个显示项目列表的组合框。 显示的项目列表由一组单选按钮确定。 我将单击事件附加到单选按钮,并尝试在组合框上设置新的项源。 我希望selectedItem默认为0,而不是-1。

我究竟做错了什么?

<Grid>
    <ComboBox Name="cb_Test" />
    <RadioButton Content="List 1" Name="radioButton1" Click="radioButton1_Click" />
    <RadioButton Content="List 2" Name="radioButton2" Click="radioButton2_Click" />
</Grid>
public partial class MainWindow : Window
{
    List<string> list1 = new List<string>() { "list 1", "list 1", "list 1" };
    List<string> list2 = new List<string>() { "list 2", "list 2", "list 2" };
    ComboBoxViewModel viewModel = new ComboBoxViewModel();

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = viewModel;
        cb_Test.ItemsSource = list1;
    }

    private void radioButton1_Click(object sender, RoutedEventArgs e)
    {
        cb_Test.ItemsSource = list1;
        viewModel.SelectedIndex = 0;
    }

    private void radioButton2_Click(object sender, RoutedEventArgs e)
    {
        cb_Test.ItemsSource = list2;
        viewModel.SelectedIndex = 0;
    }
}

public class ComboBoxViewModel : INotifyPropertyChanged
{
    private int selectedIndex;
    public event PropertyChangedEventHandler PropertyChanged;

    public int SelectedIndex
    {
        get { return selectedIndex; }
        set
        {
            if (selectedIndex != value)
            {
                selectedIndex = value;
                NotifyPropertyChanged("SelectedIndex");
            }
        }
    }

    private void NotifyPropertyChanged(string controlName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(controlName));
        }
    }
}

您的XAML中没有对ComboBoxViewModel视图模型的任何数据绑定,至少提供了一个。 我认为这就是问题所在。

您需要将组合框绑定到VM。

public partial class MainWindow : Window
{
    List<string> list1 = new List<string>() { "list 1", "list 1", "list 1" };
    List<string> list2 = new List<string>() { "list 2", "list 2", "list 2" };
    ComboBoxViewModel viewModel = new ComboBoxViewModel();

    public MainWindow()
    {
        InitializeComponent();
        cb_Test.DataContext = viewModel;
    }

    private void radioButton1_Click(object sender, RoutedEventArgs e)
    {
        viewModel.ItemsSource = list1;
        viewModel.SelectedIndex = 0;
    }

    private void radioButton2_Click(object sender, RoutedEventArgs e)
    {
        viewModel.ItemsSource = list2;
        viewModel.SelectedIndex = 0;
    }
}

在XAML中

<Grid>
    <ComboBox Name="cb_Test"
              ItemsSource="{Binding ItemsSourse}"
              SelectedIndex="{Binding SelectedIndex}"/>
    <RadioButton Content="List 1" Name="radioButton1" Click="radioButton1_Click" />
    <RadioButton Content="List 2" Name="radioButton2" Click="radioButton2_Click" />
</Grid>

更好的设计是移动代码viewModel.ItemsSource = list1; viewModel.SelectedIndex = 0; viewModel.ItemsSource = list1; viewModel.SelectedIndex = 0; 虚拟机本身。

同意提格伦。 无论这是什么,都需要将ComboBoxViewModel分配为Window / Page / Control的DataContext,并且需要在xaml的ComboBox的声明中绑定SelectedIndex。 由于需要绑定,因此还应该将List集合移动到具有SelectedIndex的一个ViewModel,并将ComboBox ItemsSource绑定到ViewModel中的一个属性,该属性可以在某些条件下设置。

暂无
暂无

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

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