简体   繁体   English

WPF启用基于MultiDataTrigger的禁用边框

[英]WPF Enable Disable Border based on MultiDataTrigger not working

I have a border in WPF that I am trying to enable or disable based on two properties in the viewmodel: ConnectedVisibility and OperatingMode. 我试图根据视图模型中的两个属性(在ConnectedVisibility和OperatingMode中)启用或禁用WPF中的边框。 This data trigger disables the border when connectedvisibility visibility is not set to "Visible". 当已连接的可见性未设置为“可见”时,此数据触发器将禁用边框。 But it does not work for OperatingMode. 但是它不适用于OperatingMode。 For OperatingMode other than 0, the border should be disabled but it stays enabled. 对于OperatingMode而不是0,应禁用边框,但边框保持启用状态。 It appears that there is no impact of OperatingMode changing its value at all. 似乎OperatingMode更改其值完全没有影响。 Even the breakpoints that I put in the convertor are not being hit except when the program first starts up. 除非程序首次启动,否则连插入转换器的断点都不会被击中。 The bindings look Ok as there is no problem shown in Debug output for these bindings. 绑定看起来不错,因为这些绑定在Debug输出中没有显示问题。 Any help is appreciated. 任何帮助表示赞赏。

The Style is 风格是

<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>

The convertor used in the style is -> 样式中使用的转换器是->

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();
    }
}

The XAML where this style is applied is 应用此样式的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>

The properties to which it is bound are 它绑定到的属性是

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;
    }
}

since you have only one condition to enable the same, so perhaps setting IsEnabled to False by default should do the trick 由于您只有一个条件可以启用相同条件,因此默认情况下将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>

I have added <Setter Property="IsEnabled" Value="False"/> in the style which will by default disable the control and I have removed the other redundant conditions 我添加了<Setter Property="IsEnabled" Value="False"/>样式,默认情况下将禁用该控件,并删除了其他冗余条件

so as result when the both conditions in MultiDataTrigger will meet it will enable the same, otherwise it remains disabled 因此,当MultiDataTrigger中的两个条件都满足时,它将启用相同的条件,否则将保持禁用状态

above example assumes that both of the property in MultiDataTrigger ConnectedVisibility & OperatingMode are notifying the changes. 上面的示例假定MultiDataTrigger ConnectedVisibilityOperatingMode中的两个属性都在通知更改。


you may also need to add the notification for OperatingMode in order for MultiDataTrigger to fire the trigger 您可能还需要为OperatingMode添加通知,以便MultiDataTrigger触发触发器

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

Is your OperatingMode property set implementation call RaisePropertyChanged("OperatingMode"); 您的OperatingMode属性集实现是调用RaisePropertyChanged(“ OperatingMode”);吗?

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM