如何通过XAML,WPF中的数据绑定设置VisualState INITIALIZATION

时间:2017-12-20 07:17:29

标签: c# wpf xaml

我想知道我可以用XAML做什么,用正确的VisualState初始化我的UI元素。 不在背后的代码。因为我知道如何使用C#代码。

我的UI元素的XAML在某种程度上是这样的:

<Border x:Name="PART_Border" >

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CriticalnessStates">
            <VisualState x:Name="NonCritical"/>
            <VisualState x:Name="Critical"/>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

    <i:Interaction.Triggers>

        <ei:DataTrigger Binding="{Binding IsCritical}" Value="False">
            <ei:GoToStateAction StateName="NonCritical"/>
        </ei:DataTrigger>

        <ei:DataTrigger Binding="{Binding IsCritical}" Value="True">
            <ei:GoToStateAction StateName="Critical"/>
        </ei:DataTrigger>

    </i:Interaction.Triggers>

</Border>

在加载UI元素之后 IsCritical 属性设置时,它可以正常工作,但问题是,我需要元素,加载正确 VisualState;我的意思是,

IsCritical = true =&gt;元件载荷处于临界状态
IsCritical = false =&gt;元素在非关键状态下加载

通过C#代码隐藏文件是可行的,您可以为元素编写一个加载的事件处理程序,它通过检查相应的DataContext属性来设置可视状态。我想知道如何使用纯XAML来实现这一目标。

顺便说一下, ei i 指向这些XML命名空间:

的xmlns:I =&#34; HTTP://schemas.microsoft.com/expression/2010/interactivity"的xmlns:EI =&#34; HTTP://schemas.microsoft.com/expression/2010/interactions"

感谢。

2 个答案:

答案 0 :(得分:2)

我实施了行为类:

using System;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Interactivity;
using System.Windows.Markup;

[ContentProperty("Conditions")]
public class VisualStateInitializer : Behavior<FrameworkElement>
{
    private bool _useTransitions = true;
    public bool UseTransitions
    {
        get { return _useTransitions; }
        set { _useTransitions = value; }
    }

    public List<VisualStateCondition> Conditions { get; set; }

    public VisualStateInitializer()
    {
        Conditions = new List<VisualStateCondition>();
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Loaded += AssociatedObject_Loaded;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
    }

    private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
    {
        if (AssociatedObject.DataContext == null)
            return;

        foreach (var item in Conditions)
        {
            var dataContextType = AssociatedObject.DataContext.GetType();
            System.Reflection.PropertyInfo propertyInfo;
            object value = null;

            var properties = item.PropertyName.Split('.');

            if (properties.Length > 1)
            {
                for (int i = 0; i < properties.Length; i++)
                {
                    if(value != null)
                    {
                        dataContextType = value.GetType();
                        propertyInfo = dataContextType.GetProperty(properties[i]);
                        value = propertyInfo.GetValue(value);
                    }
                    else
                    {
                        propertyInfo = dataContextType.GetProperty(properties[i]);
                        value = propertyInfo.GetValue(AssociatedObject.DataContext);
                    }
                }
            }
            else
            {
                value = AssociatedObject.DataContext.GetType().GetProperty(item.PropertyName).GetValue(AssociatedObject.DataContext);
            }

            if (value != null && value.Equals(item.Value))
                VisualStateManager.GoToElementState(AssociatedObject, item.DesiredState, UseTransitions);
        }
    }
}

VisualStateCondition 类是这样的:

public class VisualStateCondition
{
    /// <summary>
    /// The PropertyName to look for in the associated object's <see cref="FrameworkElement.DataContext"/>.
    /// </summary>
    public string PropertyName { get; set; }
    /// <summary>
    /// The Value to check for equality.
    /// </summary>
    public object Value { get; set; }
    /// <summary>
    /// The <see cref="VisualState.Name"/> that is desired for the condition.
    /// </summary>
    public string DesiredState { get; set; }
}

我需要在正确的初始状态下初始化我的View类,如下所示定义 VisualStateConditions

注意=&gt; &#34;我&#34; xaml中的命名空间是指&#34; System.Windows.Interactivity&#34; BlendSdk 的xmlns:I =&#34; HTTP://schemas.microsoft.com/expression/2010/interactivity"

<Border>

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CriticalnessStates">
            <VisualState x:Name="NonCritical"/>
            <VisualState x:Name="Critical"/>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

    <i:Interaction.Behaviors>
        <viewHelpers:VisualStateInitializer>

            <viewHelpers:VisualStateCondition PropertyName="IsCritical" DesiredState="Critical">
                <viewHelpers:VisualStateCondition.Value>
                    <system:Boolean>
                        True
                    </system:Boolean>
                </viewHelpers:VisualStateCondition.Value>
            </viewHelpers:VisualStateCondition>

            <viewHelpers:VisualStateCondition PropertyName="IsCritical" DesiredState="NonCritical">
                <viewHelpers:VisualStateCondition.Value>
                    <system:Boolean>
                        False
                    </system:Boolean>
                </viewHelpers:VisualStateCondition.Value>
            </viewHelpers:VisualStateCondition>

        </viewHelpers:VisualStateInitializer>
    </i:Interaction.Behaviors>

</border>

其中ViewHelpers是我的VisualStateInitializer&amp;的名称空间。 VisualStateCondition类。 任何人都可以提出更好的方法吗?

UPDATE 1 =&gt; 编辑属性值检索,以便我们可以传递以下嵌套属性:

<viewHelpers:VisualStateCondition PropertyName="X.Y.NestedProperty"
    Value="False"
    DesiredState="StateForNestedProperty"/>

答案 1 :(得分:0)

IsCritical是一个源属性,应该在视图模型或控件本身中以编程方式实现和设置。因此,应在视图模型中或在实现属性的任何位置设置默认值。

StyleDataTrigger无法设置视图模型属性的值。

此外,它是控件的实现,应该定义它所处的视觉状态。您可以通过在控件类中以编程方式调用VisualStateManager.GoToState来实现此目的。