[英]Wpf Combobox in Master/Detail MVVM
我有MVVM主/這樣的細節:
<Window.Resources>
<DataTemplate DataType="{x:Type model:EveryDay}">
<views:EveryDayView/>
</DataTemplate>
<DataTemplate DataType="{x:Type model:EveryMonth}">
<views:EveryMonthView/>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Margin="12,24,0,35" Name="schedules"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Elements}"
SelectedItem="{Binding Path=CurrentElement}"
DisplayMemberPath="Name" HorizontalAlignment="Left" Width="120"/>
<ContentControl Margin="168,86,32,35" Name="contentControl1"
Content="{Binding Path=CurrentElement.Schedule}" />
<ComboBox Height="23" Margin="188,24,51,0" Name="comboBox1"
VerticalAlignment="Top"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Schedules}"
SelectedItem="{Binding Path=CurrentElement.Schedule}"
DisplayMemberPath="Name"
SelectedValuePath="ID"
SelectedValue="{Binding Path=CurrentElement.Schedule.ID}"/>
</Grid>
這個窗口有DataContext類:
public class MainViewModel : INotifyPropertyChanged {
public MainViewModel() {
elements.Add(new Element("first", new EveryDay("First EveryDay object")));
elements.Add(new Element("second", new EveryMonth("Every Month object")));
elements.Add(new Element("third", new EveryDay("Second EveryDay object")));
schedules.Add(new EveryDay());
schedules.Add(new EveryMonth());
}
private ObservableCollection<ScheduleBase> _schedules = new
ObservableCollection<ScheduleBase>();
public ObservableCollection<ScheduleBase> Schedules {
get {
return _schedules;
}
set {
schedules = value;
this.OnPropertyChanged("Schedules");
}
}
private Element _currentElement = null;
public Element CurrentElement {
get {
return this._currentElement;
}
set {
this._currentElement = value;
this.OnPropertyChanged("CurrentElement");
}
}
private ObservableCollection<Element> _elements = new
ObservableCollection<Element>();
public ObservableCollection<Element> Elements {
get {
return _elements;
}
set {
elements = value;
this.OnPropertyChanged("Elements");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
其中一個觀點:
<UserControl x:Class="Views.EveryDayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid >
<GroupBox Header="Every Day Data" Name="groupBox1" VerticalAlignment="Top">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBox Name="textBox2" Text="{Binding Path=AnyDayData}" />
</Grid>
</GroupBox>
</Grid>
我在ComboBox中的SelectedItem無法正常工作。 我的代碼中是否有任何明顯的錯誤?
我通常做的是將ItemsControl
的項綁定到ICollectionView
(通常是ListCollectionView),而不是直接綁定到集合; 我認為這是默認情況下ItemsControl
作用(創建一個默認的ICollectionView
),但我可能錯了。
無論如何,這允許您使用ICollectionView
的CurrentItem
屬性,該屬性自動與ItemsControl
的選定項同步(如果控件的IsSynchronizedWithCurrentItem
屬性為true或null / default)。 然后,當您需要ViewModel中的當前項時,您可以使用它。 您還可以使用ICollectionView上的MoveCurrentTo...
方法設置所選項目。
但是當我重新閱讀這個問題時,我意識到你可能會有另一個問題; 您有一組“默認”項目,需要一種方法將它們與特定實例相匹配。 然而,如果它們屬於同一類型,則覆蓋對象的相等運算符以使它們始終相等是一個壞主意,因為這有可能使其他代碼非常混亂。 我會考慮將類型信息提取到枚舉中,並在每個對象上放置一個只讀屬性,返回一個枚舉值。 然后,您可以將項綁定到枚舉值的集合,並將所選項綁定到每個對象的枚舉屬性。
讓我知道如果你需要一個例子,我可能已經搞砸了解釋:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.