WPF基于MultiDataTrigger启用禁用边框不起作用

本文关键字:边框 不起作用 启用 基于 MultiDataTrigger WPF | 更新日期: 2023-09-27 18:21:45

我在WPF中有一个边界,我正试图根据视图模型中的两个属性启用或禁用它:ConnectedVisibility和OperatingMode。当连通性可见性未设置为"可见"时,此数据触发器将禁用边界。但它不适用于操作模式。对于0以外的操作模式,应禁用边框,但它保持启用状态。操作模式更改其值似乎根本没有影响。即使是我在转换器中设置的断点也不会被命中,除非程序第一次启动。绑定看起来不错,因为这些绑定的调试输出中没有显示任何问题。感谢您的帮助。

样式为

<Style x:Key="EnableOnConnectBorderCorrected" TargetType="{x:Type Border}">
    <!--<Setter Property="BorderBrush" Value="#FFDADADA"/>-->
    <Setter Property="BorderBrush" Value="Red"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="CornerRadius" Value="2"/>
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding ConnectedVisibility}" Value="Visible"/>
                <Condition Binding="{Binding OperatingMode}" Value="0"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="IsEnabled" Value="True"/>
        </MultiDataTrigger>
        <DataTrigger Binding="{Binding OperatingMode, Converter={x:Static VM:IsEqualOrGreaterThanSHORTConverter.Instance}, ConverterParameter=1,Mode=TwoWay}" Value="True">
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding ConnectedVisibility}" Value="Collapsed">
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

样式中使用的转换器是->

public class IsEqualOrGreaterThanSHORTConverter : IValueConverter
{
    public static readonly IValueConverter Instance = new IsEqualOrGreaterThanSHORTConverter();
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        short iValue = (short)value;
        short compareToValue = System.Convert.ToInt16(parameter);
        return iValue >= compareToValue;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

应用此样式的XAML是

<Border Name="RebootDash" Grid.Row="2" Grid.Column="1" Style="{StaticResource EnableOnConnectBorderCorrected}" BorderBrush="#FFDADADA" BorderThickness="1" CornerRadius="2" Width="Auto" Margin="0,1,1,0">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Border Grid.Row="2" Background="Wheat"/>
        <telerik:RadButton Command="{Binding ResetUnitCommand, Source={StaticResource UnitCommandProvider}}" Style="{StaticResource DashBoardImageButton}">
            <Image Grid.Row="0" Source="/Images/UnitManagementImages/IMG_THOR_UNITResetUnit128.png"
                   ToolTip="{x:Static properties:Resources.Unit_Command_ResetUnit}" 
                   Width="40" Height="40"
                   Margin="0,5,0,5"
                   HorizontalAlignment="Center"/>
        </telerik:RadButton>
        <TextBlock Grid.Row="2" Text="{x:Static properties:Resources.Unit_Command_ResetUnit}" HorizontalAlignment="Center" Margin="5,5,5,5"/>
    </Grid>
</Border>

它所绑定的属性是

public Visibility ConnectedVisibility
{
    get { return connectedVisibility; }
    set
    {
        if (connectedVisibility == value) return;
        connectedVisibility = value;
        RaisePropertyChanged("ConnectedVisibility");
    }
}
public short OperatingMode
{
    get { return UnitOperatingModeVM.OperatingMode; }
    set
    {
        UnitOperatingModeVM.OperatingMode = value;
    }
}

WPF基于MultiDataTrigger启用禁用边框不起作用

因为只有一个条件可以启用相同的条件,所以也许默认情况下将IsEnabled设置为False应该可以实现

<Style x:Key="EnableOnConnectBorderCorrected" TargetType="{x:Type Border}">
    <!--<Setter Property="BorderBrush" Value="#FFDADADA"/>-->
    <Setter Property="BorderBrush" Value="Red"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="CornerRadius" Value="2"/>
    <Setter Property="IsEnabled" Value="False"/>
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding ConnectedVisibility}" Value="Visible"/>
                <Condition Binding="{Binding OperatingMode}" Value="0"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="IsEnabled" Value="True"/>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

我已经在默认情况下禁用控制的样式中添加了<Setter Property="IsEnabled" Value="False"/>,并且我已经删除了其他冗余条件

因此,当MultiDataTrigger中的两个条件都满足时,它将启用相同的条件,否则它将保持禁用

以上示例假设MultiDataTrigger ConnectedVisibility&CCD_ 4正在通知这些变化。


您可能还需要添加OperatingMode的通知,以便MultiDataTrigger触发

public short OperatingMode
{
    get { return UnitOperatingModeVM.OperatingMode; }
    set
    {
        UnitOperatingModeVM.OperatingMode = value;
        RaisePropertyChanged("OperatingMode");
    }
}

是OperatingMode属性集实现调用RaisePropertyChanged("OperatingMode");

private short operatingMode;
public short OperatingMode
{
    get
    {
        return operatingMode;
    }
    set
    {
        if (operatingMode != value)
        {
            operatingMode = value;
            this.RaisePropertyChanged("OperatingMode");
        }
    }
}