c# - WPF/XAML VisualStateManager 谁处理事件?

标签 c# wpf xaml

在我的 CustomControl 中,我使用这样的 VisualStateManager:

...
<ControlTemplate TargetType="{x:Type local:MyCustomControl}">
    <Grid x:Name="rootGrid" >
        <Grid.RowDefinitions>
            <RowDefinition Height="65"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CheckStates">
                <VisualState x:Name="Checked">
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="toggleRegionContent"
                                         Storyboard.TargetProperty="Height"
                                         From="0"
                                         To="{TemplateBinding ContentHeight}"
                                         Duration="0:0:.7" />
                        <DoubleAnimation Storyboard.TargetName="arrowIcon"
                                         Storyboard.TargetProperty="(Path.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)"
                                         From="0"
                                         To="180"
                                         Duration="0:0:.1" />
                        </Storyboard>
                <VisualState>
                <!-- Some other States -->
            </VisualStateGroup>
        </VisualStateManager>
    <ToggleButton>
<!-- The Rest of the Control -->

我现在的问题是如何处理已检查的事件或由谁处理它。单击 ToggleButton 时 VisualStateManager 是否自动切换状态。或者我是否必须给我的 ToggleButton 一个选中事件,并在代码隐藏中分配按钮的状态,如下所示:

VisualStateManager.GoToElementState(Control, "Checked", true);

感谢您的帮助。

最佳答案

Or do i have to give my ToggleButton a Checked Event and assign the State of the Button in the Code Behind with something like this:

是的,您的自定义控件负责使用 VisualStateManager 类及其 GoToState 方法设置控件的当前视觉状态。

您可以在 MSDN 上阅读有关此内容的更多信息:https://msdn.microsoft.com/en-us/library/ee330302(v=vs.110).aspx

关于c# - WPF/XAML VisualStateManager 谁处理事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42440605/

相关文章:

c# - IIdentity、IPrincipal 或 IUser : what's the difference?

c# - 返回null时如何递归选择AdornedElement的父级

c# - 加载 DLL 创建实例并卸载

c# - 为什么更新字符串 INotifyPropertyChanged 属性而不更新 List<string>?

c# - 表单按钮禁用外观

c# - 在 .NET 中提取图像的一部分

c# - 特定类型的数据触发器

c# - 无法将 Xaml 中的 SelectedIndex ="0"设置为 ListView(Windows 应用商店应用程序)

c# - 丢失数据上下文的附加集合项

c# - 在带有 Html Table 的网页上,如何使用 C# 确定是否选择了 Html Row?