如何从样式更改祖先的属性

时间:2019-07-23 15:36:24

标签: wpf styles

我的按钮具有样式,当我的按钮单击事件从样式激发而没有任何代码时,我想将窗口的WindowState属性更改为最大化。 我已经尝试过了,但是没有用。 并且此错误出现了: “无法解析属性路径'WindowState'中的所有属性引用。请验证适用的对象是否支持这些属性。”

请问您有什么工作想法。

<Window x:Class="MyTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyTestApp"
        mc:Ignorable="d"
        x:Name="ss"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="Button">
            <Style.Triggers>               
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard >
                            <ObjectAnimationUsingKeyFrames 
                                Storyboard.Target="{Binding RelativeSource={RelativeSource AncestorType=Window ,Mode=FindAncestor }}" 
                                Storyboard.TargetProperty="WindowState"
                                >
                                <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <WindowState>Maximized</WindowState>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>

                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>

                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Width="200" Height="200" Background="Aqua">
        <Button Content="Click me !" Width="100" Height="50"/>
        </StackPanel>
    </Grid>
</Window> 

更新1:

我有一个新的问题。我创建了两个按钮,一个用于最大化状态,一个用于正常状态。如果我仅使用这两个按钮就没有问题。我试图通过窗口的最大化按钮最大化窗口,然后我的样式化普通按钮不起作用!如果我通过单击窗口的最大化按钮来最大化窗口,则样式化的普通按钮不起作用。因此,如果我单击样式化的最大化按钮,则样式化的常规按钮将再次起作用。怎么了?

<Window x:Class="MyTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyTestApp"
        StateChanged="ss_StateChanged"
        mc:Ignorable="d"
        x:Name="ss"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="Button" x:Key="MaximizedButtonStyle">
            <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}"/>
            <Style.Triggers>              

                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard >
                            <ObjectAnimationUsingKeyFrames  Storyboard.TargetProperty="Tag.WindowState"                        >
                                <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <WindowState>Maximized</WindowState>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>

                </EventTrigger>
            </Style.Triggers>
        </Style>

        <Style TargetType="Button" x:Key="NormalButtonStyle">
            <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}"/>
            <Style.Triggers>              
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard >
                            <ObjectAnimationUsingKeyFrames  Storyboard.TargetProperty="Tag.WindowState"                        >
                                <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <WindowState>Normal</WindowState>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>

                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Width="200" Height="200" Background="Aqua">
            <Button x:Name="b1" Style="{StaticResource NormalButtonStyle}" Content="Normal Click me !" Width="100" Height="50" />
            <Button x:Name="b2" Style="{StaticResource MaximizedButtonStyle}" Content="maximize eClick me !" Width="100" Height="50" />            
        </StackPanel>
    </Grid>
</Window> 

1 个答案:

答案 0 :(得分:1)

如果您在Visual Studio的“输出”窗格中查看,则会看到RelativeSource失败。我认为这可能会失败,因为情节提要不在视觉树中。绑定最终在Button本身上查找WindowState。

这里有一个对我有用的稍微难看的解决方案:在情节提要中,所有可以“看到”的绑定是按钮,而不是可视树的其余部分。因此,我们会将Window走私到Button的属性中。一种更受人尊敬的方法是使用BindingProxy。绑定代理是一种有用的技术,值得结识。

<Style TargetType="Button" x:Key="MinimizeButtonStyle">
    <Setter 
        Property="Tag" 
        Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}" 
        />
    <Style.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard >
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetProperty="Tag.WindowState"
                        >

    <!-- the rest is the same as what you've got -->

以下是我在Output中看到的带有原始代码的错误:

  

System.Windows.Data错误:4:找不到参考'RelativeSource FindAncestor,AncestorType ='System.Windows.Window',AncestorLevel ='1''的绑定源。 BindingExpression :(无路径); DataItem = null;目标元素是“ ObjectAnimationUsingKeyFrames”(HashCode = 51442863); target属性为“ Target”(类型为“ DependencyObject”)

     

System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“按钮”(名称=“)”上找不到“ WindowState”属性。空

     

抛出异常:PresentationFramework.dll中的'System.InvalidOperationException'

     

PresentationFramework.dll中发生了'System.InvalidOperationException'类型的未处理异常

     

无法解析属性路径“ WindowState”中的所有属性引用。验证适用的对象支持这些属性。