运行时绑定更改+意外事件触发问题WPF

时间:2014-04-12 07:59:21

标签: wpf xaml

我在运行时使用XAML中的datatrigger更改绑定,如下所示:

<ToggleButton Checked="MinSyncX_OnChecked" Unchecked="MinSyncX_OnUnchecked" TargetUpdated="FrameworkElement_OnTargetUpdated"
        Tag="{Binding Path=CameraViewDirection, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
        ToolTip="{Binding Source={StaticResource CameraLocalization}, Path=ToolTips.SyncX, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
    <ToggleButton.Style>
        <Style TargetType="ToggleButton" BasedOn="{StaticResource CameraSyncCommonStyle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=CameraViewDirection.Limitations.UI.SelectedLimitation.W, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="0">
                    <Setter Property="IsEnabled" Value="{Binding Path=CameraViewDirection.Limitations.UI.IsMinTripleEnabled.X, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></Setter>
                    <Setter Property="IsChecked" Value="{Binding Path=CameraViewDirection.Limitations.UI.IsMinTripleChecked.X, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=CameraViewDirection.Limitations.UI.SelectedLimitation.W, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="1">
                    <Setter Property="IsEnabled" Value="{Binding Path=CameraViewDirection.Limitations.UI.IsMaxTripleEnabled.X, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></Setter>
                    <Setter Property="IsChecked" Value="{Binding Path=CameraViewDirection.Limitations.UI.IsMaxTripleChecked.X, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=CameraViewDirection.Limitations.UI.SelectedLimitation.W, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="2">
                    <Setter Property="IsEnabled" Value="{Binding Path=CameraViewDirection.Limitations.UI.IsBothTripleEnabled.X, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></Setter>
                    <Setter Property="IsChecked" Value="{Binding Path=CameraViewDirection.Limitations.UI.IsBothTripleChecked.X, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ToggleButton.Style>
    <ToggleButton.Content>
        <Image Source="{Binding Source={StaticResource CameraIcons}, Path=XAxis, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></Image>
    </ToggleButton.Content>
</ToggleButton>

一切似乎都很好 - 绑定确实发生了变化,但我有一个问题,我不知道如何解决:

  • 更改绑定后,即使绑定值为true,MinSyncX_OnUnchecked事件也会触发,这意味着切换按钮必须保持选中状态。

  • 在绑定切换期间触发此事件会使我的代码失败,所以我现在希望如果IsChecked属性必须设置为true,如何在绑定更改期间禁止此事件。

有点棘手,但我希望你能理解我的问题。

修改

有一点我注意到,如果数据触发值切换到列表中的第一个,则未经检查的事件会一直触发。即使它有一个值= 1或值= 2。 这是设计还是可以某种方式修复?

1 个答案:

答案 0 :(得分:1)

我意识到我有双重绑定,因此,当绑定更改时,我的绑定属性首先从按钮接收值(这就是未经检查的事件触发的原因)。我找到的解决方案是:通过一些bool值锁定未检查事件的逻辑,并在绑定更改时切换该值(在我的情况下,在combobx上选择更改事件)