选定的ComboBoxItem图像不会更改

时间:2016-07-06 15:34:23

标签: c# wpf xaml combobox

我将此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");
}

但这样做不对。该列表现在显示图标,但不显示当前项目本身。

No image

Image

我错过了哪一步?

为了看到它,我必须选择另一个菜单项,然后回到它。

谢谢。

更新

我改变了我的两个模型属性:

[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,对吧?

1 个答案:

答案 0 :(得分:1)

铃声图像的可见性似乎是由名为DisplayBellImage的属性的值驱动的。我没有看到你改变该属性的价值,或者为它提出PropertyChanged。每次OnPropertyChanged("DisplayBellImage");的值发生变化时,您都应该致电DisplayBellImage