WPF 绑定与转换器

发布于 2024-08-16 16:04:39 字数 4432 浏览 3 评论 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 的绑定是什么?

I've been recently playing with WPF and I've come across a number of problems that I can't solve. I have the following code in my 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>

I can't get Binding #1 to work. I want to bind the trigger to the IsPressed property of the button, what should the binding be? Also what should the binding be for Binding #2 if I want to pass the button's Width and Height to the converter?

Also I could set the trigger this way instead:

<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>

Which one is better and what would the binding be for Binding #1 and #2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2024-08-23 16:04:39

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

您的第二种方法更接近标记,但仍然错误,再次使用触发器的 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)上。

Both approaches are a little bit off. Your first approach is struggling to reach the IsPressed property using binding, however the Property property of the Trigger object is not a DependencyProperty so it doesn't support binding.

Your second approach is closer to the mark but still wrong, again uses binding on the Property property of the Trigger.

Check this out instead:

<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>

Notice that I put the trigger logic on the control template level (specifying the target element), not on the individual element (the TextBlock in this case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文