我有一个这样定义的组合框
<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)SelectedValue
到Room
您的 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/