0

I have a combobox that shows a list of items. The list of items displayed is determined by a set of radio buttons. I attach to the radio button clicked events and attempt to set a new itemssource on the combobox. I would like the selectedItem to default to 0, and not -1.

What am I doing wrong?

<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));
        }
    }
}
4

3 回答 3

1

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

于 2011-08-01T19:36:18.543 回答
0

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

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 本身。

于 2011-08-01T19:37:37.233 回答
0

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

于 2011-08-01T19:38:19.457 回答