2012-08-10 45 views
0

我有一个窗口与组合框。这个comboboxhas有5个ComboboxItems。绑定SelectedIndex(Combobox)不能从代码隐藏文件工作

我将SelectedItem(combobox)绑定到我的代码隐藏文件中的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 
    } 
} 

(我知道与属性已启用,我可以解决这个问题,但我也不是什么在这里)

回答

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。

0

什么情况是:

  • 你选择一个项目。
  • 绑定调用MainWindow.SelectedIndex.set()
  • OnPropertyChanged指示属性的更改...但WPF已经设置此属性,因此信息被解散。

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

我有这个问题了很多,完成绑定都的SelectedItem和SelectedIndex的......越来越经常失去了:(之间

+0

哦..谢谢,我会尝试 – David 2012-08-10 07:15:54

+0

我以前从未有这样的问题。只要绑定到SelectedItem就没有任何东西被解雇。另一种可能性是将itemssource绑定到http://msdn.microsoft.com/de-de/library/system.windows.data.collectionview.aspx,因此collectionview会包含selecteditem自动查看http:// msdn .microsoft.com/DE-DE /库/ system.windows.data.collectionview.currentitem.aspx – 2012-08-10 07:16:38

0

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

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