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

时间:2011-08-01 19:32:02

标签: c# wpf combobox itemssource selectedindex

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您需要将组合框绑定到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;移动到VM本身。

答案 2 :(得分:0)

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