我有绑定到ComboBox
的ObservableCollection
:
<ComboBox x:Name="comboMeetingWeek" ItemsSource="{Binding Meetings}"
SelectedItem="{Binding Meeting, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Images/Bell.png" Margin="0,0,5,0"
Visibility="{Binding DisplayBellImage, Converter={StaticResource BoolToHiddenConverter}}" Stretch="None"/>
<TextBlock Text="{Binding DateMeetingAsText}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我正在执行的这个
ContextMenu
(正在进行的工作):<Image Grid.Column="1" HorizontalAlignment="Right" Source="Images\event_time.png" Margin="2">
<Image.ContextMenu>
<ContextMenu>
<MenuItem Header="Set Special Event" Command="{Binding SetSpecialEventCommand, Mode=OneWay}"/>
<MenuItem Header="Edit Special Event" Command="{Binding EditSpecialEventCommand, Mode=OneWay}"/>
<MenuItem Header="Remove Special Event">
<MenuItem.Style>
<Style TargetType="MenuItem">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Meeting.IsSpecialEvent}" Value="True">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
</ContextMenu>
</Image.ContextMenu>
</Image>
现在暂时忽略删除特殊事件。
这是两个关联的命令:
_SetSpecialEventCommand = new DelegateCommand<string>(
(s) =>
{
SpecialEventWindow windowEvent = new SpecialEventWindow();
windowEvent.DataContext = this;
windowEvent.ShowDialog();
if (windowEvent.DialogResult.HasValue && windowEvent.DialogResult.Value)
_Meeting.IsSpecialEvent = true;
},
(s) => !_Meeting.IsSpecialEvent);
_EditSpecialEventCommand = new DelegateCommand<string>(
(s) =>
{
SpecialEventWindow windowEvent = new SpecialEventWindow();
windowEvent.DataContext = this;
windowEvent.ShowDialog();
if (windowEvent.DialogResult.HasValue && windowEvent.DialogResult.Value)
_Meeting.IsSpecialEvent = true;
else
_Meeting.IsSpecialEvent = false;
},
(s) => _Meeting.IsSpecialEvent);
并且,以便
ContextMenu
下次可以正确设置菜单状态:public Data.MeetingInfo.Meeting Meeting
{
get { return _Meeting; }
set
{
// Has the existing meeting object changed at all?
if(_Meeting != null && _Meeting.IsDirty)
{
// Yes, so save it
_Model.Serialize();
_Meeting.MarkClean();
}
// Now we can update to new value
if (value != null)
{
_Meeting = value;
OnPropertyChanged("Meeting");
_SetSpecialEventCommand.RaiseCanExecuteChanged();
_EditSpecialEventCommand.RaiseCanExecuteChanged();
}
}
}
private Data.MeetingInfo.Meeting _Meeting;
因此,我的上下文菜单现在始终与当前组合菜单项的状态匹配。
但是,当我将组合项目设置为以前没有的事件时,该组合项目不会更新并添加图像。我决定将ItemSource设置为也有一个
UpdateTriggerSource=PropertyChanged
。然后,我调整了监听事件处理程序:
private void Meeting_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnPropertyChanged(e.PropertyName);
if (e.PropertyName == "IsSpecialEvent" || e.PropertyName == "IsCircuitVisit")
OnPropertyChanged("Meetings");
}
但这是行不通的。列表现在使用图标显示它,但不显示当前项目本身。
我想走哪一步?
为了看到它,我必须选择另一个菜单项,然后再回到它。
谢谢。
更新资料
我更改了两个模型属性:
[XmlIgnore]
public bool IsSpecialEvent
{
get { return _SpecialEvent.Event; }
set
{
_SpecialEvent.Event = value;
MarkDirty();
OnPropertyChanged("IsSpecialEvent");
OnPropertyChanged("DisplayBellImage");
}
}
[XmlIgnore]
public bool IsCircuitVisit
{
get { return _SpecialEvent.CircuitVisit; }
set
{
_SpecialEvent.CircuitVisit = value;
MarkDirty();
OnPropertyChanged("IsCircuitVisit");
OnPropertyChanged("DisplayBellImage");
}
}
他们现在还触发了DisplayBellImage,一切似乎都很好。但是我确实需要像以前那样更新ItemSource,对吗?
最佳答案
钟形图像的可见性似乎由称为DisplayBellImage
的属性的值驱动。我看不到您曾经更改过该属性的值,也没有提高它的PropertyChanged
。每当OnPropertyChanged("DisplayBellImage");
的值更改时,您都应该调用DisplayBellImage
。