【问题标题】:GoToState not working on controltemplated usercontrol based on comboboxGoToState 不适用于基于组合框的 controltemplated 用户控件
【发布时间】:2010-09-06 07:36:35
【问题描述】:

我正在尝试创建一个自定义组合框,该组合框在设置了 bool 属性(例如 IsLoading)时显示加载动画。

我在 Blend 中基于组合框创建了一个控件模板,并在切换按钮模板中添加了一个文本块。在后面的代码中,我调用了 VisualStateManager.GoToState,但它总是返回 false。我最初试图移动到自定义状态,但我什至无法移动到 Disabled 或 MouseOver 等状态。

我有一个只包含一个组合框的用户控件,并且样式设置为包含控件模板的组合框样式。我认为 GoToState 失败是因为状态不在控件本身上,而是在组合框中。我怎样才能访问它?

我不知道如何调试它,因为没有错误。

谢谢

【问题讨论】:

    标签: silverlight user-controls combobox controltemplate visualstatemanager


    【解决方案1】:

    我遇到了类似的问题!我在 ControlTemplate 的网格中定义了视觉状态。

            <Style x:Key="Image3TextRowButtonStyle" TargetType="Button">
            <Setter Property="Foreground" Value="{StaticResource ForegroundBrush}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid x:Name="RootGrid" Background="{StaticResource BackgroundBrush}">
                            ...
                            <VisualStateManager.VisualStateGroups>
                                ...
                                <VisualStateGroup x:Name="ActiveStates">
                                    <VisualState x:Name="Active">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background)" Storyboard.TargetName="RootGrid">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource BackgroundActiveBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="NotActive" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    以及用户控件:

    <UserControl x:Class="Griesser.Presentation.ContactCenterClient.Client.Control.Image3TextRowButtonUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             x:Name="Image3TextRowButton">
    
    <Grid>
        <Button x:Name="btn" Command="{Binding Command, ElementName=Image3TextRowButton}" Style="{StaticResource Image3TextRowButtonStyle}" />
    </Grid>
    

    在后面的代码中,我首先寻找 RootGrid,然后使用 GoToElementState:

            private void ChangeVisualActiveState(bool useTransitions)
        {
            // Search RootGrid, because the Visual States are defined in the ControlTemplate!
            FrameworkElement dt = btn.Template.FindName("RootGrid", btn) as FrameworkElement;
    
            if (IsActive)
            {
                VisualStateManager.GoToElementState(dt, "Active", useTransitions);
            }
            else
            {
                VisualStateManager.GoToElementState(dt, "NotActive", useTransitions);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 2014-09-13
      • 1970-01-01
      • 2016-10-20
      • 2021-04-28
      • 2020-01-07
      • 2022-11-11
      相关资源
      最近更新 更多