我有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中的My SelectedItem无法正常工作。我的代码中是否有任何明显的错误?
答案 0 :(得分:0)
我通常做的是将ItemsControl
的项绑定到ICollectionView
(通常是ListCollectionView),而不是直接绑定到集合;我认为这是默认情况下ItemsControl
所做的事情(创建默认ICollectionView
),但我可能错了。
无论如何,这允许您使用CurrentItem
的{{1}}属性,该属性会自动与ICollectionView
中的所选项同步(如果ItemsControl
属性控件的结果为true或null / default)。然后,当您需要ViewModel中的当前项时,您可以使用它。您还可以使用ICollectionView上的IsSynchronizedWithCurrentItem
方法设置所选项目。
但是当我重新阅读这个问题时,我意识到你可能会有另一个问题;您有一组“默认”项目,需要一种方法将它们与特定实例相匹配。然而,如果它们属于同一类型,则覆盖对象的相等运算符以使它们始终相等是一个坏主意,因为这有可能使其他代码非常混乱。我会考虑将类型信息提取到枚举中,并在每个对象上放置一个只读属性,返回一个枚举值。然后,您可以将项绑定到枚举值的集合,并将所选项绑定到每个对象的枚举属性。
如果您需要一个例子,请告诉我,我可能会弄清楚:)