2009-05-08 18 views
4

"ウィンドウ全体とその上のすべてのコントロールを調光"する効果を得ようとしています。WPFで無効なボタンの背景色を変更することはできますか?

ウィンドウとそのすべても無効にする必要があります。

ボタンが無効になっていると、背景色を変更できないように見えるという問題があります。

無効になっていてもボタンの背景色を変更する方法はありますか?

XAML:背後に

<Window x:Class="TestDimWindows.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 

    <Grid x:Name="dimElement"> 
     <StackPanel HorizontalAlignment="Left"> 
      <TextBlock 
       Text="This is an example of dimming a window." 
       Margin="5"/> 
      <StackPanel 
       HorizontalAlignment="Left" 
       Margin="5"> 
       <Button x:Name="theButton" 
         Content="Dim the window" 
         Click="Button_Click"/> 
      </StackPanel> 
     </StackPanel> 
    </Grid> 

</Window> 

コード:

using System.Windows; 
using System.Windows.Media; 

namespace TestDimWindows 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      dimElement.Background = new SolidColorBrush(Colors.Gray); 
      dimElement.Opacity = 0.5; 
      dimElement.IsEnabled = false; 

      //I want this button to look "dimmed out" as well 
      //but since it is disabled, it is a ghostly white. 
      //how can I change the color even though it is disabled? 
      theButton.Background = new SolidColorBrush(Colors.Gray); 
     } 
    } 
} 

答えて

1

あなたはそれのためにあなた自身のコントロールテンプレートを作成することができます。

私は現在使用しているテンプレートのコピーを作成するために、Blend(ライセンスをお持ちでない場合は体験版を入手できます)を使用することをお勧めします。

現在のテンプレートを調べる場合は、無効にする背景色をどこかに設定する必要があります。 IsEnabledプロパティに基づいてトリガを探します。

1

あなたはonclickを削除して色を変更し、それをあなたの「無効」状態にすることができます。

0

VisualBrushを見てください。 VisualBrushのビジュアルをコントロールに設定することができ、VisualBrushは実際の機能を持たずにコントロールのビジュアル表現を再作成します。

私はこの例をSells/Griffithsの "Programming WPF"(グラフィックスの第13章)から取り出し、それを少し変更して、もう少し解説しました。

これは、単純な描画インターフェイス(2点の線を結ぶ2点のx座標とy座標を入力)を作成するだけでなく、下の画像も反映します。それはまったく頑強ではありませんが、あなたが探していると思う機能を実証するはずです。
VisualBrushとSolidColorBrushを持つ最後の2つの矩形要素は、複製されたコントロールを作成し、それを陰にする方法です。

あなたができることは、陰影を探しているページ/ウィンドウ/ etcにこれらの2つの要素をオーバーレイし、エフェクトを実行するときに表示させることです。

これが役に立ちます。

<Window x:Class="GraphicsTest.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="300"> 
<Window.Resources> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="2*"/> 
     <RowDefinition Height="1*"></RowDefinition> 
    </Grid.RowDefinitions> 
    <Grid x:Name="mainUI"> 
     <DockPanel> 
      <GroupBox Header="Point 1" Name="gbPoint1" DockPanel.Dock="Top"> 
       <StackPanel Orientation="Horizontal"> 
        <Label Width="40" HorizontalContentAlignment="Right">X:</Label> 
        <TextBox Name="tbX1" Width="40" TextChanged="Content_TextChanged"/> 
        <Label Width="40" HorizontalContentAlignment="Right">Y:</Label> 
        <TextBox Name="tbY1" Width="40" TextChanged="Content_TextChanged"/> 
       </StackPanel> 
      </GroupBox> 
      <GroupBox Header="Point 2" Name="gbPoint2" DockPanel.Dock="Top"> 
       <StackPanel Orientation="Horizontal"> 
        <Label Width="40" HorizontalContentAlignment="Right">X:</Label> 
        <TextBox Name="tbX2" Width="40" TextChanged="Content_TextChanged"/> 
        <Label Width="40" HorizontalContentAlignment="Right">Y:</Label> 
        <TextBox Name="tbY2" Width="40" TextChanged="Content_TextChanged"/> 
       </StackPanel> 
      </GroupBox> 
      <Canvas> 
       <Line Name="lnDraw" Stroke="Black" StrokeThickness="2"/> 
      </Canvas> 
     </DockPanel> 
    </Grid> 
    <Rectangle Grid.Row="1"> 
     <Rectangle.LayoutTransform> 
      <ScaleTransform ScaleY="-1"/> 
     </Rectangle.LayoutTransform> 
     <Rectangle.Fill> 
      <VisualBrush Visual="{Binding ElementName=mainUI}" /> 
     </Rectangle.Fill> 
    </Rectangle> 
    <Rectangle Grid.Row="1"> 
     <Rectangle.Fill> 
      <SolidColorBrush Color="Black" Opacity=".5"/> 
     </Rectangle.Fill> 
    </Rectangle> 
</Grid> 

との.cs

public partial class Window1 : Window 
{ 
    public Window1() 
    { 
     InitializeComponent(); 
    } 

    private void Content_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     int x1; 
     int x2; 
     int y1; 
     int y2; 

     if (int.TryParse(tbX1.Text, out x1) && int.TryParse(tbX2.Text, out x2) && int.TryParse(tbY1.Text, out y1) && int.TryParse(tbY2.Text, out y2)) 
     { 
      lnDraw.X1 = x1; 
      lnDraw.X2 = x2; 
      lnDraw.Y1 = y1; 
      lnDraw.Y2 = y2; 
     } 
    } 
} 
0

Iはグリッド全体を埋める矩形と薄暗いアウト効果を試みる、灰色であり、1よりも小さい不透明度及びZを有しますあなたの通常のコントロールより高いインデックス。デフォルトでは、矩形はvisibility = collapsedになり、適切な "IsEnabled"プロパティが "true"になると、可視性を可視に設定するトリガを使用します。

関連する問題