3

今日のNerdPlusArtブログには、作成者が頻繁に使用する矢印のWPFリソースの作成に関する投稿がありました。戻るボタンと進むボタンがあるサイドプロジェクトがあるので、左矢印と右矢印がこれらのボタンでうまく機能すると思いました。

LeftArrowおよびジオメトリをアプリケーションのリソースに追加しRightArrow、ボタンのコンテンツとして使用しました。

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

ボタンが有効になっているかどうかに関係なく、矢印が黒で描かれていることを除いて、それは機能しました。だから、私はValueConverterからに行くためにを作成しboolましたBrush

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

(ハードコードされた黒とグレーの代わりにシステムカラーを使用する必要があることはわかっていますが、最初にこれを機能させたかっただけです。)

コンバーター(アプリケーションのリソース内で作成したもの)を使用するようにのFillプロパティを変更しました。Path

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

残念ながら、これは機能しません。理由はわかりません。実行すると、矢印がまったく描画されません。Visual Studioの[出力]ウィンドウを確認しましたが、バインドエラーは表示されませんでした。boolまた、ボタンを有効にするかどうかに基づいて、がコンバーターで正しい値であることを確認しました。

Path戻るをaに変更すると(そしてと同じ方法でTextBlockそのプロパティをバインドすると)、テキストは常に黒で描画されます。ForegroundPath.Fill

私は何か間違ったことをしていますか?コンバーターから返されたものがボタンのBrushレンダリングに使用されないのはなぜですか?Path

4

3 に答える 3

3

この種の 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 で、それぞれ左矢印と右矢印のデフォルトの塗りつぶしを設定します。パス自体に設定すると、その値が優先され、スタイルまたはそのトリガーが行う可能性のあるものはすべてオーバーライドされます。基本スタイルである ArrowStyle には、親ボタンにバインドされた DataTrigger が含まれています。これは、IsEnabled が false のときに起動され、Path の Fill をグレーに変更します。

于 2009-06-04T13:29:33.443 に答える
2

Path の Fill を Button の Foreground にバインドしないのはなぜですか?

<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>
于 2009-06-04T13:27:45.787 に答える
0

あなたのコードは基本的に機能します。これがコンバーターの取得元を指定していないため、静的リソースが間違っている可能性があると思います。

あなたはおそらく必要です

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

次に、バインディングを次のように指定します。

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, Converter={StaticResource brushConv}}
于 2009-06-04T12:06:36.373 に答える