Silverlight MVVM 组合框绑定(bind)被破坏

标签 silverlight data-binding mvvm silverlight-4.0 combobox

我有一个这样定义的组合框

<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}"/>

ViewModel 中定义了 2 个属性,RoomList 是 List,Room 属性是一个字符串。

当我第一次运行应用程序时,一切正常,并且下拉获取正确的值以及选择了正确的值。但是,在某些情况下,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 会随着新的选择而改变,但是该选择不会绑定(bind)并且我上面提到的问题开始发生。

最佳答案

您的 SelectedValuePath是“显示”,我假设它是 Room 的字符串属性类(class)。但是你正在绑定(bind)SelectedValueRoom您的 View 模型的属性,我假设此属性的类型为 Room ...所以SelectedValue是字符串类型,并且您将其绑定(bind)到类型为 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}"/>

关于Silverlight MVVM 组合框绑定(bind)被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3832059/

相关文章:

c# - Silverlight:当我调用 NavigationService.Navigate 时布局发生变化

javascript - 将输入模型绑定(bind)到数组中的每个对象

c# - WinForms:在基类中覆盖 GetHashCode 时绑定(bind)功能被破坏

c# - 具有复合集合的组合框不会随着对 observablecollection 的更改而更新

c# - 如何在路径中添加 TextBlock?

visual-studio-2008 - Silverlight 开发 [Visual Studio 2008 与 Expression Blend]

silverlight - 在页面中央制作 silverlight 对象

wpf - DataTemplate.Triggers 与 Style.Triggers

android - 在 MVVM 架构中使用 DialogFragment 的正确方法是什么?

c# - 选择 ListView 项时填充文本框