1

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

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

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

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

谢谢

4

1 回答 1

0

我有类似的问题!我在 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);
        }
    }
于 2013-08-17T10:09:29.117 回答