TextBox.Foreground.Opacity 属性的奇怪行为

发布于 2024-12-26 11:56:16 字数 2899 浏览 5 评论 0原文

我创建了一个 silverlight 模板控件。该控件由 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 bug 还是我做错了什么?

I created a silverlight template control. Thouse control consist 4 elements: 2 textbox and 2 textblock.
markup (in 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>

In code file I get the textbox from template and set Foreground.Opacity property equels 0.5.
code:

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

When I added this control in my silverlight application all textboxs element began represent text with Foreground.Opacity = 0.5
Start application:

First tab before oper login tab

Select "Login" tab:

Login tab

Back to "Some infromation" tab:

First tab after open login tab

Sample located here: http://perpetuumsoft.com/Support/silverlight/SilverlightApplicationOpacity.zip
Is it silverlight bug or I do something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2025-01-02 11:56:16

问题在于 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
}   

The problem is that the Foreground property is of type Brush which is a reference type (a class).

When you assign .Opacity = 0.5 you are changing the opacity value of the referenced Brush. All other elements that are referencing the same brush will be affected.

Ordinarily we would use a Storyboard in VisualStateManager in the control template to specify the visual appearance of a control in different "states".

However a quick fix for your code would be:

private void SetInActive()     
{     
    Brush brush = new SolidColorBrush(Colors.Black) { Opacity = 0.5 };
    _loginTextBox.Foreground = brush    
    _passwordTextBox.Foreground= brush
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文