2012-11-26 146 views
0

代码:组合框不显示所选项目

private ObservableCollection<State> allStates = new ObservableCollection<State>(); 

//国与许多属性的类,其中一人是INT '索引' 和廉政 'OneArrow'

public MainWindow() 
{ 
... 

this.MyComboBox.ItemsSource = allStates; 
this.MyComboBox.DisplayMemberPath = "Index"; 
this.MyComboBox.SelectedValuePath = "Index"; 

this.DataContext = MyState; 
} 

在xaml:

<ComboBox Name="MyComboBox" 
Width="60" Height="20" 
IsEnabled="False" 
SelectedValue="{Binding Path=OneArrow, UpdateSourceTrigger=PropertyChanged}"/> 

绑定工作,这很好,但我有另一个问题。 Combobox不会播放选定的项目。我的意思是在下拉列表中突出显示了一个正确的项目,但是当下拉列表被隐藏时,没有任何显示。

enter image description here

+0

你可以粘贴你的状态类,是“索引”状态的一部分,并且是“OneArrow”与“索引”相同的数据类型 –

+0

public int OneArrow { get {return(int)GetValue(OneArrowProperty); } {SetValue(OneArrowProperty,value);} set {SetValue(OneArrowProperty,value); } } public int Index { get { return index; } set { index = value; this.IndexTextBlock.Text =“q”+ value.ToString(); } } –

+0

OneArrow和Index都是int –

回答

0

没有看到你的类它很难猜怎么回事错了。 这是我对你的代码看起来像什么的interprtation。

的XAML:

<Window x:Class="WpfApplication4.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" Name="UI"> 
    <Grid > 
     <ComboBox ItemsSource="{Binding ElementName=UI, Path=AllStates}" 
        DisplayMemberPath="Index" 
        SelectedValuePath="Index" 
        SelectedValue="{Binding ElementName=UI, Path=OneArrow}" 
        Height="21" HorizontalAlignment="Left" Margin="80,82,0,0" Name="comboBox1" VerticalAlignment="Top" Width="152" /> 
    </Grid> 
</Window> 

代码背后:

/// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      AllStates.Add(new State { Index = 1 }); 
      AllStates.Add(new State { Index = 2 }); 
      AllStates.Add(new State { Index = 3 }); 
      AllStates.Add(new State { Index = 4 }); 
     } 

     private ObservableCollection<State> allStates = new ObservableCollection<State>(); 
     public ObservableCollection<State> AllStates 
     { 
      get { return allStates; } 
      set { allStates = value; } 
     } 

     private int oneArrow; 
     public int OneArrow 
     { 
      get { return oneArrow; } 
      set { oneArrow = value; } 
     } 

    } 

    public class State : INotifyPropertyChanged 
    { 
     private int index; 
     public int Index 
     { 
      get { return index; } 
      set { index = value; NotifyPropertyChanged("Index"); } 
     } 


     public event PropertyChangedEventHandler PropertyChanged; 
     public void NotifyPropertyChanged(string property) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 
      } 
     } 
    } 

这似乎是工作好了,但就像我说我不知道​​你的状态类是什么样子或者OneArrow位于(MainForm的或状态)