我使用MVVM模式綁定WPF中的組合框。我能夠綁定與組合框的字符串列表,但我不知道如何在組合框中設置默認值。 那麼我有一個名單,其中有「A」,「B」,「C」和「D」。現在我希望默認情況下「A」應該作爲默認值。如何使用MVVM在組合框中設置默認文本
感謝
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:WpfApplication1.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<ViewModel:NameViewModel></ViewModel:NameViewModel>
</Window.DataContext>
<Grid>
<ComboBox Height="23" Width="120" ItemsSource="{Binding Names}"/>
</Grid>
public class NameViewModel
{
private IList<string> _nameList = new List<string>();
public IList<string> Names { get; set; }
public NameViewModel()
{
Names = GetAllNames();
}
private IList<string> GetAllNames()
{
IList<string> names = new List<string>();
names.Add("A");
names.Add("B");
names.Add("C");
names.Add("D");
return names;
}
}
Set SelectedItem via another view model property? – Alan
是的Alan現在我明白了,謝謝。 – user1399377