【问题标题】:WPF - Can't set combobox selectedIndex when itemssource changesWPF - 当 itemssource 更改时无法设置组合框 selectedIndex
【发布时间】:2011-08-01 19:32:01
【问题描述】:

我有一个显示项目列表的组合框。显示的项目列表由一组单选按钮确定。我附加到单选按钮单击事件并尝试在组合框上设置新的 itemssource。我希望 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));
        }
    }
}

【问题讨论】:

  • 您的视图模型似乎没有绑定到组合框,对吧?

标签: c# wpf combobox itemssource selectedindex


【解决方案1】:

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

【讨论】:

  • 啊,真不敢相信我错过了。谢谢!
【解决方案2】:

您需要将您的组合框绑定到您的虚拟机。

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; 移动到 VM 本身。

【讨论】:

    【解决方案3】:

    同意蒂格兰。您需要将 ComboBoxViewModel 指定为 Window/Page/Control 的 DataContext,无论这是什么,并且您需要在 xaml 中的 ComboBox 声明中绑定 SelectedIndex。由于您需要绑定,您还应该使用 SelectedIndex 将 List 集合移动到一个 ViewModel,并将 ComboBox ItemsSource 绑定到 ViewModel 中您可以在某些条件下设置的 Property。

    【讨论】:

      猜你喜欢
      • 2021-07-27
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      相关资源
      最近更新 更多