質問

WPF-MVVMアプリケーションにボタン制御があります。

私は使用します ICommand プロパティ(ViewModelで定義)ボタンクリックイベントをバインドしてViewModelにバインドします。

私は - > executeを持っています canexecute 私のためのパラメーター ICommand 実装 (RelayCommand).

もしそれでも CanExecute 偽です...ボタンが無効になっていません...ボタンのコンテンツが画像の場合

ただし、ボタンのコンテンツがテキストである場合。

<Button DockPanel.Dock="Top" 
                        Command="{Binding Path=MoveUpCommand}">
                    <Button.Content>
                        <Image Source="/Resources/MoveUpArrow.png"></Image>
                    </Button.Content>
                    <Style>
                        <Style.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" Value=".5" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button>
役に立ちましたか?

解決

ボタンは無効になります。それは、それが画像のレンダリングに影響しないということです。画像の不透明度を.5に変更するスタイルでトリガーを作成する必要があります。

<Style x:Key="imageButton" TargetType="Button">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value=".5" />
            </Trigger>
        </Style.Triggers>
</Style>

他のヒント

ありがとう!すべてのボタンの後、提案されたコードを試しました。うまくいきませんでした。トリガーのみを抽出し、他のすべてのボタンが継承された一般的なボタンに挿入しようとしました。このコード最初:

<Style x:Key="SecButton" TargetType="Button">
    <Setter Property="FontSize" Value="16" />
    <Setter Property="Margin" Value="0,0,5,5" />
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value=".5" />
        </Trigger>
    </Style.Triggers>
</Style>

上記のコードに基づいて、このようなボタンを作成しました。

<Style x:Key="NewBtnStyle" TargetType="Button" BasedOn="{StaticResource SecButton}">                
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="Images/new.png" Width="50" Height="50" />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

ボタンが無効になっている場合、その中の画像が自動的に0.5の不透明度に暗くなります。

必要になるだろう ピクセルシェーダー効果 (聞こえるほど複雑ではありません。アセンブリ参照を追加するだけです。その後、任意の組み込みのWPF効果と同じくらい簡単に使用できます)。グレースケールで。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top