gpt4 book ai didi

c# - 如何通过 XAML、WPF 中的数据绑定(bind)设置 VisualState INITIALIZATION

转载 作者:行者123 更新时间:2023-12-04 16:08:04 27 4
gpt4 key购买 nike

我想知道我可以用 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>

IsCritical 属性设置加载 UI 元素时,它工作得很好,但问题是,我需要该元素, 以加载正确的 VisualState;我的意思是,

IsCritical = true => 处于临界状态的元素加载
IsCritical = false => 元素在非临界状态下加载

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

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

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

谢谢。

最佳答案

我实现了这个行为类:

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,如下所示:

注意=> xaml中的“i”命名空间是指BlendSdk的“System.Windows.Interactivity”xmlns:i="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 和 VisualStateCondition 类的命名空间。谁能提出更好的方法?

更新 1 => 编辑属性值检索,以便我们可以传递嵌套属性,例如:

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

关于c# - 如何通过 XAML、WPF 中的数据绑定(bind)设置 VisualState INITIALIZATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47900680/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com