2017-01-06 21 views
1

**編集 - 私はこれに対する答えとしてDynamicResourceの回答にフラグを立てました。これは私がここで説明した問題を解決しました。私は主なアプリケーションでまだ問題が発生していましたが、それは私がDynamicResourceとして私のボーダーで使用する前にブラシをStaticResourceとして使用していたためです。 DynamicResourceにすべてを切り替えたとき、正しく動作しました。ありがとう!WPF ToggleButton Template BorderBrush

BorderBrushがテンプレートのToggleButtonで動作するように見えません。ここに私のサンプル.xamlがあります。これを実行すると、マウスオーバーまたはボタンの1つをチェックすると、境界線が透明になることがわかります。四角形を使用して

<Window x:Class="ToggleButtonStyle.MainWindow" 
    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" 
    xmlns:local="clr-namespace:ToggleButtonStyle" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <!-- <ResourceDictionary Source="Dictionary1.xaml" /> --> 
    <!-- main color of buttons--> 
    <Color x:Key="MainThemeColor">Orange</Color> 

    <!-- hover-over color for buttons --> 
    <Color x:Key="MouseOverColor">Purple</Color> 

    <!-- 5. Mouse over background color for step buttons --> 
    <SolidColorBrush x:Key="MouseOverBackgroundBrush" Color="{DynamicResource MouseOverColor}"/> 

    <!-- 6. Background color active step --> 
    <SolidColorBrush x:Key="CheckedBackgroundBrush" Color="{DynamicResource MainThemeColor}"/> 

    <Style TargetType="{x:Type ToggleButton}" x:Key="ToggleButtonStyle"> 
     <Setter Property="Background" Value="White" /> 
     <Setter Property="Foreground" Value="Black" /> 
     <Setter Property="Width" Value="Auto"/> 
     <Setter Property="Height" Value="40"/> 
     <Setter Property="FontSize" Value="13"/> 
     <Setter Property="MinWidth" Value="80"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness ="{TemplateBinding BorderThickness}" Padding="5" Margin="2"> 
         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="BorderBrush" Value="{StaticResource CheckedBackgroundBrush}" /> 
       <Setter Property="Background" Value="{StaticResource MouseOverBackgroundBrush}" /> 
       <Setter Property="Foreground" Value="#333333" /> 
      </Trigger> 
      <Trigger Property="IsChecked" Value="True"> 
       <Setter Property="BorderBrush" Value="{StaticResource MouseOverBackgroundBrush}"/> 
       <Setter Property="Background" Value="{StaticResource CheckedBackgroundBrush}" /> 
       <Setter Property="Foreground" Value="#ffffff"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Grid> 
    <StackPanel Orientation="Horizontal"> 
     <ToggleButton Style="{DynamicResource ToggleButtonStyle}">Button 1</ToggleButton> 
     <ToggleButton Style="{DynamicResource ToggleButtonStyle}">Button 2</ToggleButton> 
     <ToggleButton Style="{DynamicResource ToggleButtonStyle}">Button 3</ToggleButton> 
    </StackPanel> 
</Grid> 

+0

WPFインスペクタまたはVisual Studio 2015を使用して、ビジュアルツリーを表示できます。そこにはどのリソースが適用され、どのスタイルからであるかがわかります。 – Michael

+0

私はそれをしました。 BorderBrushが "Transparent"であり、スタイルトリガーから来ているということだけです。 – MadSkeletor

+0

静的リソースを参照するのではなく、BorderBrushセッターを特定の値に設定しようとする可能性があります。それが動作するかどうかを確認するだけです。 – Michael

答えて

0

ブラシのColorプロパティは、あなたにもダイナミックリソース使用して、あなたのセッターでプロパティを設定する必要がありますダイナミック資源マークアップ拡張機能を使用して設定されているので:私はあなたがStaticResourceを使用

<Style.Triggers> 
    <Trigger Property="IsMouseOver" Value="True"> 
     <Setter Property="BorderBrush" Value="{DynamicResource CheckedBackgroundBrush}" /> 
     <Setter Property="Background" Value="{DynamicResource MouseOverBackgroundBrush}" /> 
     <Setter Property="Foreground" Value="#333333" /> 
    </Trigger> 
    <Trigger Property="IsChecked" Value="True"> 
     <Setter Property="BorderBrush" Value="{DynamicResource MouseOverBackgroundBrush}"/> 
     <Setter Property="Background" Value="{DynamicResource CheckedBackgroundBrush}" /> 
     <Setter Property="Foreground" Value="#ffffff"/> 
    </Trigger> 
</Style.Triggers> 

を実行時に実際に色リソースが参照されると、セッターの値は更新されません。

+0

これは 'Border'と同様に機能します。 StrangeResourceとDynamicResourceの組み合わせで、BorderBrush以外の方法でも動作することは、奇妙なことです。 – Michael

+0

**編集、私の悪い、私はちょうど間違った名前を持っていた。リソースディクショナリでも動作します – MadSkeletor

1

動作しているようです。 これを見てください:DynamicResource color doesn't work for BorderBrush on a Border - Bug?。これはうまくいかないと私には分かりません。

<Style TargetType="{x:Type ToggleButton}"> 
     <Setter Property="Background" Value="White" /> 
     <Setter Property="Foreground" Value="Black" /> 
     <Setter Property="BorderBrush" Value="{DynamicResource MouseOverBackgroundBrush}"/> 
     <Setter Property="Width" Value="Auto"/> 
     <Setter Property="Height" Value="40"/> 
     <Setter Property="FontSize" Value="13"/> 
     <Setter Property="MinWidth" Value="80"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <Grid> 
         <Rectangle Fill="{TemplateBinding Background}" 
            Stroke="{TemplateBinding BorderBrush}" 
            StrokeThickness="{TemplateBinding BorderThickness}" 
            Margin="2"> 
         </Rectangle> 
         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="BorderBrush" Value="{StaticResource CheckedBackgroundBrush}" /> 
       <Setter Property="Background" Value="{StaticResource MouseOverBackgroundBrush}" /> 
       <Setter Property="Foreground" Value="#333333" /> 
      </Trigger> 
      <Trigger Property="IsChecked" Value="True"> 
       <Setter Property="BorderBrush" Value="{StaticResource MouseOverBackgroundBrush}"/> 
       <Setter Property="Background" Value="{StaticResource CheckedBackgroundBrush}" /> 
       <Setter Property="Foreground" Value="#ffffff"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
関連する問題