0

我最近一直在玩 WPF,并且遇到了许多我无法解决的问题。我的 generic.xaml 中有以下代码:

<Style TargetType="{x:Type local:ClearButton}">      
    <Style.Resources>
        <con:ValueConverter x:Key="converter" />
    </Style.Resources>
    <Setter Property="Width" Value="20" />
    <Setter Property="Height" Value="20" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ClearButton}">
                <Grid>

                    <Image Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                        <Image.Style>
                            <Style TargetType="{x:Type Image}">
                                <Setter Property="Source" Value="/WPF-Libraries;component/Resources/ClearEnabled.png" />
                                <Style.Triggers>   

                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="BitmapEffect">
                                            <Setter.Value>
                                                <OuterGlowBitmapEffect Opacity="0.5" GlowColor="Red" GlowSize="3" />
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>

                                    <Trigger Property="IsEnabled" Value="False">
                                        <Setter Property="Source" 
                                                Value="/WPF-Libraries;component/Resources/ClearDisabled.png" />
                                    </Trigger>

                                    <!--Binding #1-->
                                    <Trigger Property="{TemplateBinding local:ClearButton.IsPressed}" Value="True">
                                        <Setter Property="RenderTransform">
                                            <Setter.Value>
                                                <!--Binding #2-->
                                                <ScaleTransform CenterX="CONVERTER BINDING:PASS WIDTH TO CONVERTER" CenterY="CONVERTER BINDING:PASS HEIGHT TO CONVERTER" ScaleX="0.75" ScaleY="0.75" />
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>

                                </Style.Triggers>                        
                            </Style>
                        </Image.Style>
                    </Image>

                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我无法让 Binding #1 工作。我想将触发器绑定到按钮的 IsPressed 属性,绑定应该是什么?如果我想将按钮的宽度和高度传递给转换器,绑定#2 的绑定应该是什么?

我也可以这样设置触发器:

<Style TargetType="{x:Type local:ClearButton}">
    <Style.Resources>
        <con:ValueConverter x:Key="converter" />
    </Style.Resources>
    <Setter Property="Width" Value="20" />
    <Setter Property="Height" Value="20" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ClearButton}">
                <!--Abbreviated-->
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <!--Binding #1-->
        <Trigger Property="{TemplateBinding local:ClearButton.IsPressed}" Value="True">
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <!--Binding #2-->
                    <ScaleTransform CenterX="CONVERTER BINDING" CenterY="CONVERTER BINDING" ScaleX="0.75" ScaleY="0.75" />
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

哪个更好,绑定 #1 和 #2 的绑定是什么?

4

1 回答 1

1

两种方法都有点偏离。您的第一种方法是努力使用绑定来访问 IsPressed 属性,但是 Trigger 对象的 Property 属性不是 DependencyProperty,因此它不支持绑定。

您的第二种方法更接近标记,但仍然是错误的,再次使用 Trigger 的 Property 属性的绑定。

请检查一下:

<Style TargetType="Button" x:Key="ClearButtonStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <TextBlock 
                        Name="x" 
                        Text="I will change my color when ou press me"
                        TextAlignment="Center" 
                        VerticalAlignment="Center" 
                        Foreground="Red" 
                        TextWrapping="Wrap"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter 
                            TargetName="x" 
                            Property="Foreground" 
                            Value="Green"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

请注意,我将触发逻辑放在控件模板级别(指定目标元素),而不是放在单个元素(在本例中为 TextBlock)。

于 2009-12-27T12:09:52.790 回答