WPF:如果" IsEnabled = False"

时间:2017-07-05 09:45:17

标签: wpf xaml user-controls grid

我有一个UserControl,它由一个包含3列的网格组成:     

Name="Test">

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            ...
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<Grid Width="{StaticResource DoubleOverallWidthUserControls}" Height="{StaticResource DoubleHeightControls}">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{StaticResource GridLengthUserControlLabel}" />
        <ColumnDefinition Width="{StaticResource GridLengthUserControlContent}" />
        <ColumnDefinition Width="{StaticResource GridLengthUserControlUnits}" />
    </Grid.ColumnDefinitions>

    <Border
        Grid.Row="0"
        Grid.Column="0"
        BorderBrush="{StaticResource BrushTextInputBorder}"
        BorderThickness="0,0,0,1"
        Cursor="Arrow">
        <TextBlock
            x:Name="TextBlockLabel"
            Width="{StaticResource DoubleWidthUserControlLabel}"
            VerticalAlignment="Center"
            DockPanel.Dock="Top"
            TextAlignment="Left" />
    </Border>
    <Border BorderBrush="{StaticResource BrushBorder}" CornerRadius="3,3,0,0" Grid.Row="0"
        Grid.Column="1" Background="White">
    <TextBox
        x:Name="TextBoxText"
        Width="{StaticResource DoubleWidthUserControlContent}" />
        <Border.Style>
            <Style TargetType="Border">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=Test, Path=IsEnabled}" Value="False">
                        <Setter Property="Background" Value="Gray"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
    </Border>

    <TextBlock
        x:Name="TextBlockUnit"
        Grid.Row="0"
        Grid.Column="2"
        Width="{StaticResource DoubleWidthUserControlUnits}"
        Margin="10,0,0,0"
        VerticalAlignment="Center" />
</Grid>

现在我想在我的控件中更改以下内容,如果它被禁用:

  • 第1列:网格底部边框颜色
  • 第2列:包装透明文本框的白色边框元素的背景颜色

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

样式设定器仅在您未直接设置属性时才有效。这是因为直接赋值创建了一个所谓的本地值,它的优先级高于样式设置器的值。有关详细信息,请参阅Dependency Property Value Precedence

由于您要禁用两个边框,您可以在资源中创建以下样式:

<UserControl.Resources>
    <Style x:Key="BorderStyle" TargetType="Border">
        <Setter Property="Background" Value="White"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=Test, Path=IsEnabled}"
                         Value="False">
                 <Setter Property="Background" Value="Gray"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

将此样式指定给边框。

<Border Style="{StaticResource BorderStyle}"/>

您甚至可以通过省略x:Key

将其设置为UserControl中的默认样式
<UserControl.Resources>
    <Style TargetType="Border">
        ...
    </Style>
</UserControl.Resources>

因此,如果按钮:

,则不必显式设置Style属性
<Border>
    ...
</Border>