0

我正在尝试创建一个图像按钮,该按钮根据条件呈现不同的图像。此外,我需要另一个条件,将鼠标悬停在另一个图像上。

该示例适用于数据网格行(单元格)内的按钮,该按钮在错误条件下显示灰色十字,在鼠标悬停时显示红色十字。如果初始条件为真,则显示灰色勾号,鼠标悬停时显示绿色勾号。初始条件来自行的绑定元素。

到目前为止,我没有任何工作!在下面的示例中忽略图像的路径位置。

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Button Style="{StaticResource ExcludeIncludeButtonStyle}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

<Style x:Key="ExcludeIncludeButtonStyle" TargetType="Button">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Margin" Value="5"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Name="border" BorderThickness="0" Background="Transparent">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="15" Height="15" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter Property="Background">
                            <Setter.Value>
                                <ImageBrush ImageSource="exclude.png" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background">
                            <Setter.Value>
                                <ImageBrush ImageSource="exclude_hover.png" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

1 回答 1

1

在您Border添加所需的模板绑定中,并且您的绑定有多个条件,您必须使用MultiDataTriggers:

<ControlTemplate TargetType="Button">
    <Border Name="border" BorderThickness="0" Background="{TemplateBinding Background}">
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="15" Height="15" />
    </Border>
    <ControlTemplate.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{TemplateBinding IsMouseOver}" Value="false"/>
                <Condition Binding="{Binding InitialCondition}" Value="false"/>
            </MultiDataTrigger.Conditions>

            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="GreyCross.png" />
                </Setter.Value>
            </Setter>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{TemplateBinding IsMouseOver}" Value="true"/>
                <Condition Binding="{Binding InitialCondition}" Value="false"/>
            </MultiDataTrigger.Conditions>

            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="RedCross.png" />
                </Setter.Value>
            </Setter>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{TemplateBinding IsMouseOver}" Value="false"/>
                <Condition Binding="{Binding InitialCondition}" Value="true"/>
            </MultiDataTrigger.Conditions>

            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="GreyTick.png" />
                </Setter.Value>
            </Setter>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{TemplateBinding IsMouseOver}" Value="true"/>
                <Condition Binding="{Binding InitialCondition}" Value="true"/>
            </MultiDataTrigger.Conditions>

            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="GreenTick.png" />
                </Setter.Value>
            </Setter>
        </MultiDataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
于 2013-01-27T21:35:41.060 回答