今日の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