首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用VisualStates的WPF ControlTemplate

使用VisualStates的WPF ControlTemplate
EN

Stack Overflow用户
提问于 2011-05-26 16:14:54
回答 2查看 1.8K关注 0票数 0

我想用预定义的VisualStates创建一个ControlTemplate。我想在GoToStateActions和DataTriggers中使用它们。

我不知道这里到底出了什么问题。在我看来,绑定不是以这种方式建立的,我想是这样的。

代码语言:javascript
复制
namespace ControlTemplateVisualState
{
    using System.Windows.Controls;


    public class MyControl : ContentControl
    {
        public MyControl()
        {
            this.DefaultStyleKey = typeof(MyControl);
        }
    }
}
代码语言:javascript
复制
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ControlTemplateVisualState="clr-namespace:ControlTemplateVisualState" 
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">

    <ControlTemplate x:Key="MyControlTemplate" TargetType="{x:Type ControlTemplateVisualState:MyControl}">
        <Grid x:Name="grid" Background="Red">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="VisualStateGroup">
                    <VisualState x:Name="IsDisabledState">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid">
                                <EasingColorKeyFrame KeyTime="0" Value="#FF2BFF00"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <i:Interaction.Triggers>
                <ei:DataTrigger Binding="{Binding IsEnabled, ElementName=grid}" Value="False">
                    <ei:GoToStateAction StateName="IsDisabledState"/>
                </ei:DataTrigger>
            </i:Interaction.Triggers>
        </Grid>
    </ControlTemplate>

    <Style TargetType="{x:Type ControlTemplateVisualState:MyControl}">
        <Setter Property="Background" Value="#FF0010FF"/>
        <Setter Property="Template" Value="{StaticResource MyControlTemplate}"/>
    </Style>
</ResourceDictionary>


<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ControlTemplateVisualState.MainWindow"
    x:Name="Window"
    xmlns:local="clr-namespace:ControlTemplateVisualState"
    Title="MainWindow"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot">
        <local:MyControl IsEnabled="False"/>
    </Grid>
</Window>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-03 07:37:47

你可以试一试:

代码语言:javascript
复制
<i:Interaction.Behaviors>
      <si:DataStateBehavior Binding='{Binding IsLoggedIn}' Value='True' 
            TrueState='LoggedInState' FalseState='LoggedOutState'/>
</i:Interaction.Behaviors>

它略有不同,但即使与Silverlight一起工作,请参阅:http://expressionblend.codeplex.com/wikipage?title=Behaviors%20and%20Effects&referringTitle=Documentation

如果您使用WPF4:http://expressionblend.codeplex.com/workitem/8148,请确保获得修复的版本

票数 1
EN

Stack Overflow用户

发布于 2011-06-03 06:43:13

我不认为Blend SDK触发器和行为可以在控制模板中使用--只能在UserControls中使用:在解析XAML时,没有可以附加触发器的具体对象。(我不是100%确定这个解释,但我知道如果你有多个控制模板,你不能把混合行为放在所有的模板中。)您需要后台代码来调用VSM --这就是自定义控件的工作方式。您将使用类似于Silverlight Toolkit中的代码

代码语言:javascript
复制
public static void GoToState(Control control, bool useTransitions, params string[] stateNames)
{
    foreach (var name in stateNames)
    {
        if (VisualStateManager.GoToState(control, name, useTransitions))
            break;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6135558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档