今日、Nerd Plus Artブログでは、作成者が頻繁に使用する矢印用のWPFリソースの作成についての記事がありました。私はバックとフォワードのボタンを持つサイドプロジェクトを持っているので、左と右の矢印はそれらのボタンでうまくいくと思った。ブラシ用のWPF ValueConverterの作成
私は自分のアプリケーションのリソースに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>
矢印はかどうかに関係なく、ボタンの黒で描かれたことを除いて、働いていたこと有効になっているかどうか。だから、私はBrush
へbool
から行くために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();
}
}
(最初、私はおそらく代わりに、ハード黒とグレーのコード化のシステムカラーを使用する必要があることを認識し、私はちょうどこの作業を取得したいです。)
私は、アプリケーションのリソース内で作成された私のコンバータを()を使用するPath
のFill
プロパティを変更:
<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
プロパティをバインド)バックTextBlock
にPath
を変更した場合、テキストは常に黒で描かれています。
何か間違っていますか?なぜ私のコンバーターによって返されたBrush
は、ボタンにPath
をレンダリングするのに使用されていませんか?
あなたのコードがうまくいかない理由はわかりませんが、私はこのためにスタイルトリガーを使用していたでしょう... – Will