0

内部プロパティでトリガーする

<Button BorderBrush="Black" BorderThickness="2" x:Name="TimeButton" ClickMode="Press" Click="SetTime_Click" Height="26" HorizontalAlignment="Left" Margin="15, 0, 0, 0" Style="{StaticResource ImageButtonStyle}" ToolTip="Set Time" Width="26">
    <Button.Background>
        <ImageBrush x:Name="TimeImageBrush" ImageSource="/YCS;component/Images/Clock.png" Stretch="Uniform" TileMode="None" />                                        
    </Button.Background>  
</Button>

Button.Background プロパティの ImageBrush を、アイテムソースから簡単にバインドできる HasHours という名前のブール値に従って別のものに設定するトリガーを作成する必要があります。この物件に……。

私はこのようなことを試しました

<Button.Triggers>
    <DataTrigger Binding="{Binding HasHours}" Value="false">
        <Setter TargetName="TimeImageBrush" Property="ImageSource" Value="/YCS;component/Images/ClockRed.png"/>
    </DataTrigger>
</Button.Triggers>

しかし、それは私にこのエラーを与えます:

タイプ「ContentPresenter」で静的メンバー「ImageSourceProperty」が見つかりません。

どんな助けでも大歓迎です

4

2 に答える 2

1

This is perhaps not exactly an answer to your question.

First, i guess you won't be able to add a DataTrigger to the Triggers collection, since that only supports EventTriggers.

But, you could define the DataTrigger in the Button's Style. Here, instead of setting the ImageBrush's ImageSource property, simply set a new ImageBrush as Background.

<Button ...>
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasHours}" Value="False">
                    <Setter Property="Background">
                        <Setter.Value>
                            <ImageBrush ImageSource="/YCS;component/Images/ClockRed.png"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>                    
        </Style>
    </Button.Style>
</Button>
于 2012-08-21T09:54:47.197 に答える
0
  1. コンテンツがないため、画像を背景ではなくコンテンツとして配置します。
  2. ボタンではなく、画像のトリガーにDataTriggerを配置します。トリガーのDataContextを探す必要があります:
    つまり、次のようになります。

    <Button ... >  
      <Image ... >  
        <Image.Triggers>  
           <DataTrigger   
               Binding="{Binding Path= HasHours, RelativeSource={RelativeSource FindAncestor,  
                        AncestorType={x:Type Button}}}"   
                        Value="false" >  
               <Setter Property="ImageSource" Value="/YCS;component/Images/ClockRed.png"/>
          </DataTrigger>
        </Image.Triggers>
       </Image>
      </Button> 
    
于 2012-08-21T10:23:03.360 に答える