这是我的绑定源对象:
Public Class MyListObject Private _mylist As New ObservableCollection(Of String) Private _selectedName As String Public Sub New(ByVal nameList As List(Of String), ByVal defaultName As String) For Each name In nameList _mylist.Add(name) Next _selectedName = defaultName End Sub Public ReadOnly Property MyList() As ObservableCollection(Of String) Get Return _mylist End Get End Property Public ReadOnly Property SelectedName() As String Get Return _selectedName End Get End Property End Class
这是我的XAML:
这是代码隐藏:
Class Window1 Private obj1 As MyListObject Private obj2 As MyListObject Private odp As ObjectDataProvider Public Sub New() InitializeComponent() Dim namelist1 As New List(Of String) namelist1.Add("Joe") namelist1.Add("Steve") obj1 = New MyListObject(namelist1, "Steve") . Dim namelist2 As New List(Of String) namelist2.Add("Bob") namelist2.Add("Tim") obj2 = New MyListObject(namelist2, "Tim") odp = DirectCast(Me.FindResource("MyListObject"), ObjectDataProvider) odp.ObjectInstance = obj1 End Sub Private Sub btn_List1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_List1.Click odp.ObjectInstance = obj1 End Sub Private Sub btn_List2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_List2.Click odp.ObjectInstance = obj2 End Sub End Class
当Window首次加载时,绑定很好.ComboBox包含名称"Joe"和"Steve",默认情况下选择"Steve".但是,当我单击一个按钮将ObjectInstance切换为obj2时,ComboBox ItemsSource会在下拉列表中正确填充,但SelectedValue设置为Nothing而不是等于obj2.SelectedName.
上周我们遇到了类似的问题.它与如何SelectedValue
更新其内部结构有关.我们发现,如果你设置SelectedValue
它将不会看到我们必须改变设置SelectedItem
哪些将适当地更新每件事.我的结论是,SelectedValue
被设计用于GET操作和未设定.但这可能只是当前版本的3.5sp1 .net中的一个错误
激起一场2岁的对话:
另一种可能性,如果你想使用的字符串,是将其绑定到ComboBox的Text属性.
这有点像:
public class TestCode { private string _test; public string Test { get { return _test; } set { _test = value; NotifyPropertyChanged(() => Test); // NotifyPropertyChanged("Test"); if not using Caliburn } } }
上面的代码是双向的,所以如果你设置Test ="B"; 在代码,那么组合框将显示"B",然后如果您从下拉菜单中选择"A"先降后绑定属性将反映这一变化.
使用
UpdateSourceTrigger=PropertyChanged
在绑定中
问题:
ComboBox类使用IndexOf方法搜索指定的对象.此方法使用Equals方法确定相等性.
解:
所以,尝试通过Converter使用SelectedValue设置SelectedIndex,如下所示:
C#代码
//Converter public class SelectedToIndexConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null && value is YourType) { YourType YourSelectedValue = (YourType) value; YourSelectedValue = (YourType) cmbDowntimeDictionary.Tag; YourType a = (from dd in Helper.YourType where dd.YourTypePrimaryKey == YourSelectedValue.YourTypePrimaryKey select dd).First(); int index = YourTypeCollection.IndexOf(a); //YourTypeCollection - Same as ItemsSource of ComboBox } return null; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value!=null && value is int) { return YourTypeCollection[(int) value]; } return null; } }
XAML
祝好运!:)
类型SelectedValuePath
和SelectedValue
必须完全相同.
例如,如果SelectedValuePath
is Int16
的类型和绑定到的属性的类型SelectedValue
是int
不起作用的.
我花了好几个小时才找到这个,这就是为什么我在这么多时间之后回答问题的问题.也许像我这样有同样问题的另一个可怜的家伙可以看到它.