2009-06-04 15 views
2

今日、Nerd Plus Artブログでは、作成者が頻繁に使用する矢印用のWPFリソースの作成についての記事がありました。私はバックとフォワードのボタンを持つサイドプロジェクトを持っているので、左と右の矢印はそれらのボタンでうまくいくと思った。ブラシ用のWPF ValueConverterの作成

私は自分のアプリケーションのリソースにLeftArrowRightArrowジオメトリを追加して、ボタンの内容としてそれらを使用:

<Application x:Class="Notes.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    StartupUri="Views/MainWindow.xaml"> 
    <Application.Resources> 
     <Geometry x:Key="RightArrow">M0,0 L1,0.5 0,1Z</Geometry> 
     <Geometry x:Key="LeftArrow">M0,0.5 L1,1 1,0Z</Geometry> 
    </Application.Resources> 
</Application> 

<Button x:Name="BackButton" 
    Padding="5,5,5,5" 
    Command="{x:Static n:Commands.GoBackCommand}"> 
    <Path Data="{StaticResource LeftArrow}" Width="10" Height="8" 
     Stretch="Fill" Fill="Black"/> 
    </Button> 
<Button x:Name="ForwardButton" 
    Padding="5,5,5,5" 
    Command="{x:Static n:Commands.GoForwardCommand}"> 
    <Path Data="{StaticResource RightArrow}" Width="10" Height="8" 
     Stretch="Fill" Fill="Red" /> 
</Button> 

矢印はかどうかに関係なく、ボタンの黒で描かれたことを除いて、働いていたこと有効になっているかどうか。だから、私はBrushboolから行くためにValueConverterを作成:

class EnabledColorConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, 
     CultureInfo culture) 
    { 
     bool b = (bool)value; 
     return b ? Brushes.Black : Brushes.Gray; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

(最初、私はおそらく代わりに、ハード黒とグレーのコード化のシステムカラーを使用する必要があることを認識し、私はちょうどこの作業を取得したいです。)

私は、アプリケーションのリソース内で作成された私のコンバータを()を使用するPathFillプロパティを変更:

<Path Data="{StaticResource LeftArrow}" Width="10" Height="8" 
    Stretch="Fill" 
    Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, Converter={StaticResource EnabledColorConverter}}"/> 

残念ながら、これはワットませんork、なぜ私は分かりません。私がそれを実行すると、矢印はまったく描画されません。私はVisual Studioの出力ウィンドウをチェックし、バインディングエラーは表示されませんでした。また、ボタンを有効にするかどうかに基づいて、boolがコンバーターの正しい値であることを確認しました。

Iは(及びPath.Fillと同様に、そのForegroundプロパティをバインド)バックTextBlockPathを変更した場合、テキストは常に黒で描かれています。

何か間違っていますか?なぜ私のコンバーターによって返されたBrushは、ボタンにPathをレンダリングするのに使用されていませんか?

+0

あなたのコードがうまくいかない理由はわかりませんが、私はこのためにスタイルトリガーを使用していたでしょう... – Will

答えて

2

がなぜ無いだけでボタンのフォアグラウンドにあなたのパスの塗りつぶしをバインド:

おそらく

<Window.Resources> 
    <conv:EnabledColorConverter x:Key="brushConv" /> 
</Window.Resources> 

、その後はあなたのように結合指定が必要ですか?

<Button x:Name="BackButton" 
    Padding="5,5,5,5" 
    Command="{x:Static n:Commands.GoBackCommand}"> 
    <Path Data="{StaticResource LeftArrow}" Width="10" Height="8" 
     Stretch="Fill" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=Foreground"/> 
    </Button> 
+0

これははるかに直接的なアプローチであり、動作します。助けてくれてありがとう。 :) – Andy

0

コードは基本的に動作します。私はあなたの静的リソースが間違っているかもしれないと思っています。

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, Converter={StaticResource brushConv}} 
+0

私はApplication.Resourcesで定義されたコンバータを持っています - 私はちょうど上記のコードにそれを含めませんでした。混乱させて申し訳ありません。 – Andy

2

このような種類のUI状態の更新の場合は、代わりにトリガーを使用してください。それはあなたに値のコンバータを書くことからあなたを救うでしょう、そして、それは書くためにはるかに短いです。

はこの試してみてください。そして、

<Application x:Class="Notes.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    StartupUri="Views/MainWindow.xaml"> 
    <Application.Resources> 
     <Geometry x:Key="RightArrow">M0,0 L1,0.5 0,1Z</Geometry> 
     <Geometry x:Key="LeftArrow">M0,0.5 L1,1 1,0Z</Geometry> 

     <!-- Base Arrow Style, with a trigger to toggle it Gray when its parent button is disabled --> 
     <Style x:Key="ArrowStyle" TargetType="Path"> 
      <Setter Property="Width" Value="10"/> 
      <Setter Property="Height" Value="8"/> 
      <Setter Property="Stretch" Value="Fill"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled}" Value="False"> 
        <Setter Property="Fill" Value="Gray"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 

     <!-- Left Arrow Style, with the Left Arrow fill and data --> 
     <Style x:Key="LeftArrowStyle" BasedOn="{StaticResource ArrowStyle}" TargetType="Path"> 
      <Setter Property="Fill" Value="Black"/> 
      <Setter Property="Data" Value="{StaticResource LeftArrow}"/> 
     </Style> 

     <!-- Right Arrow Style, with the Right Arrow fill and data --> 
     <Style x:Key="RightArrowStyle" BasedOn="{StaticResource ArrowStyle}" TargetType="Path"> 
      <Setter Property="Fill" Value="Red"/> 
      <Setter Property="Data" Value="{StaticResource RightArrow}"/> 
     </Style> 
    </Application.Resources> 

    <Button x:Name="BackButton" Padding="5,5,5,5" IsEnabled="False"> 
     <Path Style="{StaticResource LeftArrowStyle}"/> 
    </Button> 
    <Button x:Name="ForwardButton" Padding="5,5,5,5"> 
     <Path Style="{StaticResource RightArrowStyle}"/> 
    </Button> 
</Application> 

を、あなたはそれぞれ、左と右の矢印のために、LeftArrowStyleとRightArrowStyleにあなたのデフォルトの塗りつぶしを設定します。 Path自体に設定すると、その値が優先され、スタイルやそのトリガーが行う可能性のあるものはすべて上書きされます。基本スタイルのArrowStyleには、親ボタンにバインドされたDataTriggerが含まれています.IISEabledがfalseの場合に起動し、PathのFillをGrayに変更します。

+0

私は、ボタンのフォアグラウンドへのバインディングのppxのアプローチは、より良いアプローチのように思えます。それでも、非常に有益な答えに感謝します。 :) – Andy

関連する問題