【问题标题】:Strange behaviour with TextBox.Foreground.Opacity propertyTextBox.Foreground.Opacity 属性的奇怪行为
【发布时间】:2012-01-13 07:50:38
【问题描述】:

我创建了一个 Silverlight 模板控件。 Thouse 控件由 4 个元素组成:2 个文本框和 2 个文本块。 标记(在 generic.xaml 中):

<Style TargetType="local:InputForm">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:InputForm">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Login" Grid.Column="0" Grid.Row="0"/>
                            <TextBlock Text="Password" Grid.Column="0" Grid.Row="1"/>
                            <TextBox x:Name="LoginTextBox" Grid.Column="1" Grid.Row="0" Text="Login..."/>
                            <TextBox x:Name="PasswordTextBox" Grid.Column="1" Grid.Row="1" Text="Password..."/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

在代码文件中,我从模板中获取文本框并设置 Foreground.Opacity 属性等于 0.5。 代码:

public class InputForm : Control
{
    private TextBox _loginTextBox;
    private TextBox _passwordTextBox;

    public InputForm()
    {
        this.DefaultStyleKey = typeof(InputForm);
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        _loginTextBox = this.GetTemplateChild("LoginTextBox") as TextBox;
        _passwordTextBox = this.GetTemplateChild("PasswordTextBox") as TextBox;

        SetInActive();
    }

    private void SetInActive()
    {
        _loginTextBox.Foreground.Opacity = .5;
        _passwordTextBox.Foreground.Opacity = .5;
    }
}

当我在 silverlight 应用程序中添加此控件时,所有文本框元素都开始表示 Foreground.Opacity = 0.5 的文本 开始申请:

选择“登录”标签:

返回“一些信息”标签:

样本位于:http://perpetuumsoft.com/Support/silverlight/SilverlightApplicationOpacity.zip 是silverlight错误还是我做错了什么?

【问题讨论】:

    标签: silverlight user-interface silverlight-4.0


    【解决方案1】:

    问题在于 Foreground 属性的类型是 Brush,它是一个引用类型(一个类)。

    当您分配.Opacity = 0.5 时,您正在更改引用的Brush 的不透明度值。引用同一画笔的所有其他元素都会受到影响。

    通常我们会在控件模板的 VisualStateManager 中使用 Storyboard 来指定控件在不同“状态”下的视觉外观。

    不过,您的代码的快速修复方法是:

    private void SetInActive()     
    {     
        Brush brush = new SolidColorBrush(Colors.Black) { Opacity = 0.5 };
        _loginTextBox.Foreground = brush    
        _passwordTextBox.Foreground= brush
    }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-10
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多