2013-07-26 101 views
1

我在我的应用程序中使用了许多ComboBox,并且所有这些工作都没有任何问题。但是,我现在找不到问题了。我已将SelectedValuePath设置为“标记”属性。但更改组合框选定项目后属性不会更新。我已经阅读过其他StackOverflow问题,但无所谓帮助。Silverlight ComboBox SelectedValue TwoWay绑定不起作用

这是XAML:

的xmlns:虚拟机= “CLR的命名空间:SilverlightApplication1”

<UserControl.DataContext> 
    <vms:MainViewModel /> 
</UserControl.DataContext> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <ComboBox Width="100" VerticalAlignment="Center" FontFamily="Segoe UI" 
     Height="30" Margin="0,5,0,0" HorizontalAlignment="Left" 
     SelectedValue="{Binding SelectedDifStatusComparer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
     SelectedValuePath="Tag"> 
     <ComboBox.Items> 
      <ComboBoxItem Tag="H" >High</ComboBoxItem> 
      <ComboBoxItem Tag="L" >Low</ComboBoxItem> 
      <ComboBoxItem Tag="E" >Equal</ComboBoxItem> 
     </ComboBox.Items> 
    </ComboBox> 
</Grid> 

这里是视图模型:

public class MainViewModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     private void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     private string _selectedDifStatusComparer = ""; 
     private string SelectedDifStatusComparer 
     { 
      get { return _selectedDifStatusComparer; } 
      set 
      { 
       _selectedDifStatusComparer = value; 
       MessageBox.Show(_selectedDifStatusComparer); 
       OnPropertyChanged("SelectedDifStatusComparer"); 
      } 
     } 

     public MainViewModel() 
     { 
      SelectedDifStatusComparer = "E"; // It is working, the MessageBox is apperaing 
     } 
    } 
+0

请检查你的输出窗口绑定错误或使用史努比在运行时检查你的绑定 – blindmeis

+0

@blindmeis它的工作,我只是忘了让公共财产。 –

回答

1

你的财产私人的。将其更改为公开,它应该可以工作。

+2

谢谢。我不能承认这一点。 :)看来,我累了。 –

2

你的财产是私人的。将其更改为公开,它应该可以工作。

public class MainViewModel : INotifyPropertyChanged 
     { 
      public event PropertyChangedEventHandler PropertyChanged; 
      private void OnPropertyChanged(string propertyName) 
      { 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
       } 
      } 

      private string _selectedDifStatusComparer = ""; 
      public string SelectedDifStatusComparer 
      { 
       get { return _selectedDifStatusComparer; } 
       set 
       { 
        _selectedDifStatusComparer = value; 
        MessageBox.Show(_selectedDifStatusComparer); 
        OnPropertyChanged("SelectedDifStatusComparer"); 
       } 
      } 

      public MainViewModel() 
      { 
       SelectedDifStatusComparer = "E"; // It is working, the MessageBox is apperaing 
      } 
     } 
+1

谢谢。但是我6天前已经接受了另一个答案:) –

相关问题