2010-09-30 51 views
1

我有这样定义Silverlight的MVVM组合框绑定越来越坏

<ComboBox Name="RoomDropDown" Visibility="{Binding Path=RoomDropDownVisible,Mode=OneWay,Converter={StaticResource BoolVisibilityConvertor}}" 
          ItemsSource="{Binding Path=RoomList,Mode=OneWay}" DisplayMemberPath="display" SelectedValuePath="display" SelectedValue="{Binding Path=Room,Mode=TwoWay}"/> 

组合框有在视图模型,RoomList这是清单及产权房是一个字符串定义2个属性。

第一次当我运行应用程序时,一切正常,下拉得到正确的值以及正确的值被选中。但是,在某些情况下,RoomList属性会更改为不同的来源& Room属性也会更改。现在发生的问题是组合框显示正确的值,但所选值未被选中。更糟糕的是,我们可以忍受这一点,但设置器不发射当在DropDown手动更改值。

任何关于这里出了什么问题的指针?

跟帖: 不要以为我设法传达出确切的问题,这里是我想补充说明问题的一些示例代码:

<Grid x:Name="LayoutRoot" Background="White"> 
    <StackPanel VerticalAlignment="Center" Width="100"> 
     <ComboBox Name="TestBox" Height="20" Width="100" ItemsSource="{Binding Path=ComboSource}" DisplayMemberPath="display" SelectedValuePath="code" 
        SelectedValue="{Binding Path=ComboSelection,Mode=TwoWay}"/> 
     <Button Content="Click Here" Click="Button_Click" /> 
    </StackPanel> 
</Grid> 



public MainPage() 
    { 
     InitializeComponent(); 
     this.Loaded += (s, e) => 
      { 
       var temp = new List<Binding>(); 
       temp.Add(new Binding() { code = "1", display = "One" }); 
       temp.Add(new Binding() { code = "2", display = "Two" }); 
       this.ComboSource = temp; 
       this.ComboSelection = "1"; 
       this.DataContext = this; 
      }; 
    } 

    private static readonly DependencyProperty ComboSelectionProperty = 
     DependencyProperty.Register("ComboSelectionProperty", typeof(string), typeof(MainPage), new PropertyMetadata(null)); 

    public string ComboSelection 
    { 
     get { return (string)GetValue(ComboSelectionProperty); } 
     set 
     { 
      SetValue(ComboSelectionProperty, value); 
      this.RaisePropertyChanged("ComboSelection"); 
     } 
    } 

    private static readonly DependencyProperty ComboSourceProperty = 
     DependencyProperty.Register("ComboSourceProperty", typeof(List<Binding>), typeof(MainPage), new PropertyMetadata(null)); 

    public List<Binding> ComboSource 
    { 
     get 
     { 
      return (List<Binding>)GetValue(ComboSourceProperty); 
     } 
     set 
     { 
      SetValue(ComboSourceProperty, value); 
      this.RaisePropertyChanged("ComboSource"); 
     } 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     var temp = new List<Binding>(); 
     temp.Add(new Binding() { code = "3", display = "Three" }); 
     temp.Add(new Binding() { code = "4", display = "Four" }); 

     this.ComboSource = temp; 
     this.ComboSelection = "3"; 

    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

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

    #endregion 
} 

public class Binding 
{ 
    public string code {get; set;} 
    public string display { get; set; } 
} 

没有严格MVVM,但解释问题,当按钮点击事件被触发时,Combosource会随着新的选择被更改,但是该选择不会被绑定,并且上面提到的问题开始发生。

回答

1

您的SelectedValuePath是“display”,我认为它是Room类的字符串属性。但你绑定SelectedValue到你的视图模型的Room属性,并且我假设这个属性的类型是Room ...所以SelectedValue是字符串类型,并且你将它绑定到Room类型的属性:它可以不起作用,因为这些类型之间没有转换。

而不是使用SelectedValue属性,为什么不使用SelectedItem

<ComboBox Name="RoomDropDown" Visibility="{Binding Path=RoomDropDownVisible,Mode=OneWay,Converter={StaticResource BoolVisibilityConvertor}}" 
      ItemsSource="{Binding Path=RoomList,Mode=OneWay}" DisplayMemberPath="display" SelectedItem="{Binding Path=Room,Mode=TwoWay}"/> 
+0

在上面的问题中增加了后续部分。 – user457485 2010-10-01 09:31:13

0

ComboBox数据绑定似乎存在一个错误,如果绑定到SelectedValue的数据变为null,绑定将完全中断。

在ComboSelection setter中放置一个断点,看它是否设置为null。如果这是问题的根源,它添加到您的二传手:

public string ComboSelection 
{ 
    // ..... 
    set 
    { 
     if(value == null) 
      return; 
     // ..... 
    } 
} 

在一个侧面说明,你可能不需要使用依赖属性来支持ComboSelection。只要你继续使用PropertyChanged,数据绑定就可以正常工作。