使背景对于禁用的TextBox透明

时间:2014-02-11 10:56:59

标签: c# wpf background textbox

我在TextBox中禁用Grid

GridBackgroundColor,我希望TextBox只显示一些文字。

如何将Background个已停用的TextBox设为Transparent

以下是我的尝试:

<UserControl x:Class="MyProject.Views.TextBoxView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="435"
             d:DesignWidth="589"
             FontFamily="Arial"
             mc:Ignorable="d">
    <Grid Margin="0"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch">
        <Grid.Resources>
            <Style x:Key="TextboxStyle_uniqueName" TargetType="{x:Type TextBox}">

                <Setter Property="Background" Value="Transparent" />
                <Style.Triggers>
                    <Trigger Property="Control.IsEnabled" Value="False">
                        <Setter Property="Background" Value="Transparent" />
                    </Trigger>
                    <Trigger Property="IsReadOnly" Value="True">
                        <Setter Property="Background" Value="Transparent" />
                    </Trigger>
                </Style.Triggers>

            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="36" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="20*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Row="0"
              Grid.RowSpan="2"
              Grid.Column="1"
              Grid.ColumnSpan="2"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              Background="Red">
            <Grid.RowDefinitions>
                <RowDefinition Height="40" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <TextBox Grid.Row="0"
                     Grid.Column="0"
                     Width="Auto"
                     Height="Auto"
                     Margin="0"
                     HorizontalAlignment="Right"
                     VerticalAlignment="Stretch"
                     HorizontalContentAlignment="Center"
                     VerticalContentAlignment="Center"
                     BorderThickness="0"
                     FontFamily="Courier New"
                     FontSize="30"
                     Foreground="Black"
                     IsEnabled="False"
                     IsReadOnly="True"
                     IsTabStop="False"
                     Style="{StaticResource TextboxStyle_uniqueName}"
                     Text="BLAHBLAHBLAH" />
        </Grid>
    </Grid>
</UserControl>

如果Textbox已启用,则一切正常并设置Background有效。 但是,一旦我禁用它,我就无法再正确设置Background

考虑绑定IsEnabled的一些黑客攻击,以便之前设置Background

- 编辑 -

使用TextBlock足以解决问题。但我确定项目中没有style设置的背景与TextBox的默认背景不同。

0 个答案:

没有答案