2012-01-13 26 views
2

Silverlightテンプレートコントロールを作成しました。コントロールは、2つのテキストボックスと2つのテキストブロックの4つの要素で構成されています。 (generic.xamlで) マークアップ:TextBox.Foreground.Opacityプロパティでの不思議な動作

<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をequels。 コードは:

First tab before oper login tab

を選択し、 "ログイン" タブ:私は私のSilverlightアプリケーションでこのコントロールを追加すると

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

すべてtextboxs要素はForeground.Opacityでテキストを表す= 0.5 Startアプリケーションを開始しました:

ここにある

First tab after open login tab

サンプル:http://perpetuumsoft.com/Support/silverlight/SilverlightApplicationOpacity.zip それはSilverlightのバグですか、私は間違って何かをする戻る "いくつかのinfromation" タブに?

答えて

1

問題は、Foregroundプロパティが参照型(クラス)であるタイプBrushであることです。

.Opacity = 0.5を割り当てると、参照先のBrushの不透明度値が変更されます。同じブラシを参照している他のすべての要素が影響を受けます。

通常、コントロールテンプレートのVisualStateManagerのストーリーボードを使用して、さまざまな「状態」のコントロールの外観を指定します。

しかし、あなたのコードのための簡単な修正は次のようになります。

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

多くのおかげで、それは私を助けて – Sergey

関連する問題