绑定SelectedIndex(Combobox)不能从codebehind文件中工作

时间:2012-08-10 07:06:09

标签: c# wpf data-binding combobox selectedindex

我有一个带有组合框的窗口。这个组合框有5个ComboboxItems。

我将SelectedItem(组合框)绑定到我的代码隐藏文件中的ComboBoxSelectedIndex属性。

在示例中,我希望无法选择项目4和5.

但我可以选择第4项和第5项。出了什么问题?

xaml代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterScreen"
        Height="350"
        Width="500">
    <StackPanel VerticalAlignment="Center">
        <ComboBox SelectedIndex="{Binding Path=ComboBoxSelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <ComboBoxItem>Item 1</ComboBoxItem>
            <ComboBoxItem>Item 2</ComboBoxItem>
            <ComboBoxItem>Item 3</ComboBoxItem>
            <ComboBoxItem>Item 4</ComboBoxItem>
            <ComboBoxItem>Item 5</ComboBoxItem>
        </ComboBox>     
    </StackPanel>
</Window>

代码隐藏文件:

namespace WpfApplication1
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        private int _selectedIndex;

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public int ComboBoxSelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                if (value < 3)
                {
                    _selectedIndex = value;
                }
                OnPropertyChanged("ComboBoxSelectedIndex");
                Trace.WriteLine(ComboBoxSelectedIndex);
            }
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

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

        #endregion
    }
}

(我知道我可以通过属性Is Enabled来解决这个问题。但我不知道那里的内容)

3 个答案:

答案 0 :(得分:0)

会发生什么:

  • 您选择了一个项目。
  • binding调用MainWindow.SelectedIndex.set()
  • OnPropertyChanged表示属性更改...但WPF已经设置了此属性,因此信息将被解除。

这是一个非常痛苦的WPF绑定问题...... 你可以做的是等待WPF完成在组合框中设置属性,然后触发NotifyPropertyChange。这可以在setter中创建一个新线程,以便在调度程序线程中进行通知。

我遇到了很多这个问题并完成了对SelectedItem和SelectedIndex的绑定......两者之间经常丢失:(

答案 1 :(得分:0)

创建自定义组合框以实现此

<StackPanel Orientation="Vertical">
    <wpfProj:ExtendedCombobBox 
       SelectedIndex="{Binding Path=ComboBoxSelectedIndex, 
                              Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
       MaxSelectedIndex="{Binding Path=MaxSelectedIndex}">
        <ComboBoxItem>Item 1</ComboBoxItem>
        <ComboBoxItem>Item 2</ComboBoxItem>
        <ComboBoxItem>Item 3</ComboBoxItem>
        <ComboBoxItem>Item 4</ComboBoxItem>
        <ComboBoxItem>Item 5</ComboBoxItem>
    </wpfProj:ExtendedCombobBox>
</StackPanel>

编码自定义组合框

public class ExtendedCombobBox:ComboBox
{
    public static readonly DependencyProperty MaxSelectedIndexProperty =
        DependencyProperty.Register("MaxSelectedIndex", typeof (int), typeof (ExtendedCombobBox), new PropertyMetadata(default(int)));

    public int MaxSelectedIndex
    {
        get { return (int) GetValue(MaxSelectedIndexProperty); }
        set { SetValue(MaxSelectedIndexProperty, value); }
    }

    protected override void OnSelectionChanged(SelectionChangedEventArgs e)
    {
        if (Items.IndexOf(e.AddedItems[0]) > MaxSelectedIndex)
            e.Handled = true;
        else
            base.OnSelectionChanged(e);
    }
}

<强> UPD1。 或者您可以使用标准的并订阅SelectionChanged事件。但我更喜欢使用custombobox。

答案 2 :(得分:0)

返回值不是int32而是字符串。

public string ComboBoxSelectedIndex
    {
        get { return _selectedIndex; }
        set
        {
            if (int.parse(value) < 3)
            {
                _selectedIndex = value;
            }
            OnPropertyChanged("ComboBoxSelectedIndex");
            Trace.WriteLine(ComboBoxSelectedIndex);
        }
    }