gpt4 book ai didi

c# - VisualStateManager WPF 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 23:01:11 25 4
gpt4 key购买 nike

我专门在 VS Blend 2017 设计器中设计了一个窗口,没有对 XAML 代码应用任何更改。我创建了一个 StateGroup 和一个 State 并记录了 Button 上的更改。尝试从代码隐藏应用状态时,没有任何反应。

我也看过关于同一个问题的主题,但不幸的是没有一个对我有用!

代码隐藏:

VisualStateManager.GoToState(this, "State1", false);

XAML:

<Window x:Class="Wpf_2017.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:Wpf_2017"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="MyStates">
<VisualState x:Name="State1">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button">
<EasingColorKeyFrame KeyTime="0" Value="#FFEA8686"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="button">
<EasingColorKeyFrame KeyTime="0" Value="#FFFF4848"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Button x:Name="button" Content="Button" Margin="208.306,116.798,218.093,0" VerticalAlignment="Top" Height="26.065" Click="button_Click"/>

</Grid>
</Window>

感谢您的帮助!

最佳答案

我认为您应该使用 GoToElementState 而不是 GoToState。

请试试这个,

将名称添加到网格中:

<Window x:Class="WpfApplication1.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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="MyGrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="MyStates">
<VisualState x:Name="State1">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="button">
<EasingColorKeyFrame KeyTime="0" Value="Red"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="button">
<EasingColorKeyFrame KeyTime="0" Value="Blue"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Button x:Name="button" Content="Button" Margin="208.306,116.798,218.093,0" VerticalAlignment="Top" Height="26.065" Click="button_Click"/>

</Grid>
</Window>

并在代码隐藏中使用它。

        /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void button_Click(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToElementState(this.MyGrid, "State1", false);
}
}
}

关于c# - VisualStateManager WPF 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43017841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com