WPF 组合框 SelectedIndex 属性绑定不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13389926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:13:18  来源:igfitidea点击:

WPF Combobox SelectedIndex Property Binding Not working

wpfwpf-4.0

提问by Jatin

I am trying to bind the SelectedIndex property of combobox to my ViewModel. Here is the code.

我正在尝试将组合框的 SelectedIndex 属性绑定到我的 ViewModel。这是代码。

Xaml:

Xml:

<ComboBox x:Name="BloodGroupFilter" SelectedIndex="{Binding Path=SelectedBloodGroupIndex, Mode=TwoWay}">
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ComboBoxItem Foreground="red" FontStyle="Italic">No Filter</ComboBoxItem>
            <CollectionContainer Collection="{Binding Source={StaticResource BloodGroupEnum}}"/>
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

ViewModel

视图模型

private int _selectedBloodGroupIndex = 4;
public int SelectedBloodGroupIndex {
    get { return _selectedBloodGroupIndex; }
    set { 
        _selectedBloodGroupIndex = value; 
    }
}

As you can see I am trying to set the SelectedIndex of combobox to "4". This doesn't happen and SelectedIndex is set to 0. Also, when user selects a particular item of the combobox, I was expecting that the ViewModel's SelectedBloodGroupIndex property will update itself to the currently selected item of combobox, but this doesn't happen either. The ViewModel property is never invoked(both set and get). Any reasons why binding is failing for the above code.

如您所见,我正在尝试将组合框的 SelectedIndex 设置为“4”。这不会发生并且 SelectedIndex 设置为 0。此外,当用户选择组合框的特定项目时,我期望 ViewModel 的 SelectedBloodGroupIndex 属性将自身更新为当前选择的组合框项目,但这也不会发生. ViewModel 属性永远不会被调用(设置和获取)。上述代码绑定失败的任何原因。

Update

更新

<UserControl.Resources>
    <ObjectDataProvider x:Key="BloodGroupEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="enums:BloodGroup" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</UserControl.Resources>

回答by yo chauhan

You need to Notify Property changed in the setter of SelectedBloodGroupIndexof your ViewModel . I hope you do have the idea of PropertyChanged event.

您需要在ViewModel的SelectedBloodGroupIndex的设置器中通知属性已更改。我希望你确实有 PropertyChanged 事件的想法。

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:myWindow="clr-namespace:WpfApplication4"
    Title="MainWindow" Height="800" Width="800" WindowStartupLocation="CenterScreen">

<Grid>
    <ComboBox SelectedIndex="{Binding SelectedIndex}">
        <ComboBoxItem Content="1"/>
        <ComboBoxItem Content="2"/>
        <ComboBoxItem Content="3"/>
        <ComboBoxItem Content="4"/>
        <ComboBoxItem Content="5"/>
    </ComboBox>
</Grid>

 public partial class MainWindow :Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MyViewModel();
    }
}

public class MyViewModel :INotifyPropertyChanged
{
    public MyViewModel()
    {
        SelectedIndex = 2;
    }
    private int _selectedIndex;
    public int SelectedIndex 
    { 
        get
        {
            return _selectedIndex;
        }
        set
        {
            _selectedIndex = value;
            Notify("SelectedIndex");
        }
  }

    public event PropertyChangedEventHandler PropertyChanged;

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