【发布时间】:2019-07-23 15:36:24
【问题描述】:
我的 Button 有一个 Style 我想在我的 Button Click Event Fired from style 没有任何代码的情况下将我的窗口的 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>
【问题讨论】:
标签: wpf