我有一個ComboBox
綁定到一個簡單的ViewModel
人的名單。 SelectedPerson被設置在ViewModel的構造函數中,但是當我運行應用程序時,ComboBox沒有設置其初始值。我究竟做錯了什麼?組合框不自動選擇初始值
請注意,MyPerson
類的兩個實例具有相同的ID時應視爲相等。
重要提示:我無法修改MyPerson
以覆蓋Equals(它是第三方)。
我目前看到的唯一選擇是使用適配器模式來包裝此類的實例並在其中實現自定義Equals方法。
我覺得應該有一個更好的WPF原生方法,通過某種「關鍵」從列表中匹配項目。就我而言,項目列表和所選項目來自不同的來源,這就是爲什麼他們有一個Id屬性充當主鍵。
請看看代碼,我玩過SelectedValue和SelectedValuePath,但沒有任何效果。
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Test"
mc:Ignorable="d"
x:Name="Root"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<ComboBox ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}" SelectedValuePath="Id"
DisplayMemberPath="Name"
SelectedValue="{Binding SelectedPerson}" />
</Window>
而這個視圖模型爲的DataContext:
public class MainViewModel
{
public MainViewModel()
{
SelectedPerson = new MyPerson { Name = "Mary", Id = 1 };
}
public MyPerson SelectedPerson { get; set; }
public IEnumerable<MyPerson> People { get; } = new List<MyPerson>()
{
new MyPerson() {Name = "Mary", Id = 1 },
new MyPerson() {Name = "John", Id = 2 },
};
}
public class MyPerson
{
public string Name { get; set; }
public int Id { get; set; }
}
1)首先爲您的MainViewModel使用'INotifyPropertyChanged'接口。 2)添加'OnPropertyChange'(或者你會調用它)到'selectedPerson'設置器 – Shakra
1)首先爲你的MainViewModel使用'INotifyPropertyChanged'接口。 2)添加'OnPropertyChange'(或者你會調用它)到'selectedPerson'設置器 3)你以錯誤的方式使用'SelectedValue' - > [Article](http://www.codeproject.com/Articles/ 671544/Understanding-SelectedValue-SelectedValuePath-Sele) 4)更好地使用SelectedItemIndex來檢查選擇的項目。 Atm你提供名爲Mary和Id = 1的不同對象的'SelectedPerson'和'People'列表,這可能會帶來一些問題。 *由於5分鐘的限制,無法編輯))* – Shakra
@Shakra在這種情況下,我不需要實現接口。在第一次加載組合時,應該查詢該值而不更改通知。 – SuperJMN