MultiDataTrigger 如何知道其 ConditionCollection 中的条件何时更新?

发布于 2024-11-16 08:49:44 字数 3217 浏览 2 评论 0原文

我正在尝试创建一个具有类似于 MultiDataTrigger 的 ConditionCollection 的行为。我已经查看了反射的代码,但我似乎无法理解 MultiDataTrigger 如何知道何时通过其逻辑来确定是否满足条件。

这是代码:

[ContentProperty("Setters")]
public sealed class MultiDataTrigger : TriggerBase, IAddChild
{
// Fields
private ConditionCollection _conditions = new ConditionCollection();
private SetterBaseCollection _setters;

// Methods
internal override bool GetCurrentState(DependencyObject container, UncommonField<HybridDictionary[]> dataField)
{
    bool flag = base.TriggerConditions.Length > 0;
    for (int i = 0; flag && (i < base.TriggerConditions.Length); i++)
    {
        flag = base.TriggerConditions[i].ConvertAndMatch(StyleHelper.GetDataTriggerValue(dataField, container, base.TriggerConditions[i].Binding));
    }
    return flag;
}

internal override void Seal()
{
    if (!base.IsSealed)
    {
        base.ProcessSettersCollection(this._setters);
        if (this._conditions.Count > 0)
        {
            this._conditions.Seal(ValueLookupType.DataTrigger);
        }
        base.TriggerConditions = new TriggerCondition[this._conditions.Count];
        for (int i = 0; i < base.TriggerConditions.Length; i++)
        {
            if ((this._conditions[i].SourceName != null) && (this._conditions[i].SourceName.Length > 0))
            {
                throw new InvalidOperationException(SR.Get("SourceNameNotSupportedForDataTriggers"));
            }
            base.TriggerConditions[i] = new TriggerCondition(this._conditions[i].Binding, LogicalOp.Equals, this._conditions[i].Value);
        }
        for (int j = 0; j < this.PropertyValues.Count; j++)
        {
            PropertyValue value2 = this.PropertyValues[j];
            value2.Conditions = base.TriggerConditions;
            switch (value2.ValueType)
            {
                case PropertyValueType.Trigger:
                    value2.ValueType = PropertyValueType.DataTrigger;
                    break;

                case PropertyValueType.PropertyTriggerResource:
                    value2.ValueType = PropertyValueType.DataTriggerResource;
                    break;

                default:
                    throw new InvalidOperationException(SR.Get("UnexpectedValueTypeForDataTrigger", new object[] { value2.ValueType }));
            }
            this.PropertyValues[j] = value2;
        }
        base.Seal();
    }
}

void IAddChild.AddChild(object value)
{
    base.VerifyAccess();
    this.Setters.Add(Trigger.CheckChildIsSetter(value));
}

void IAddChild.AddText(string text)
{
    base.VerifyAccess();
    XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);
}

// Properties
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ConditionCollection Conditions
{
    get
    {
        base.VerifyAccess();
        return this._conditions;
    }
}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public SetterBaseCollection Setters
{
    get
    {
        base.VerifyAccess();
        if (this._setters == null)
        {
            this._setters = new SetterBaseCollection();
        }
        return this._setters;
    }
}

}

I'm trying to make a behavior had a ConditionCollection similar to MultiDataTrigger. I've looked through the reflected code, but I can't seem to follow how the MultiDataTrigger knows when to go through it's logic to determine if the conditions are met.

Here's the code:

[ContentProperty("Setters")]
public sealed class MultiDataTrigger : TriggerBase, IAddChild
{
// Fields
private ConditionCollection _conditions = new ConditionCollection();
private SetterBaseCollection _setters;

// Methods
internal override bool GetCurrentState(DependencyObject container, UncommonField<HybridDictionary[]> dataField)
{
    bool flag = base.TriggerConditions.Length > 0;
    for (int i = 0; flag && (i < base.TriggerConditions.Length); i++)
    {
        flag = base.TriggerConditions[i].ConvertAndMatch(StyleHelper.GetDataTriggerValue(dataField, container, base.TriggerConditions[i].Binding));
    }
    return flag;
}

internal override void Seal()
{
    if (!base.IsSealed)
    {
        base.ProcessSettersCollection(this._setters);
        if (this._conditions.Count > 0)
        {
            this._conditions.Seal(ValueLookupType.DataTrigger);
        }
        base.TriggerConditions = new TriggerCondition[this._conditions.Count];
        for (int i = 0; i < base.TriggerConditions.Length; i++)
        {
            if ((this._conditions[i].SourceName != null) && (this._conditions[i].SourceName.Length > 0))
            {
                throw new InvalidOperationException(SR.Get("SourceNameNotSupportedForDataTriggers"));
            }
            base.TriggerConditions[i] = new TriggerCondition(this._conditions[i].Binding, LogicalOp.Equals, this._conditions[i].Value);
        }
        for (int j = 0; j < this.PropertyValues.Count; j++)
        {
            PropertyValue value2 = this.PropertyValues[j];
            value2.Conditions = base.TriggerConditions;
            switch (value2.ValueType)
            {
                case PropertyValueType.Trigger:
                    value2.ValueType = PropertyValueType.DataTrigger;
                    break;

                case PropertyValueType.PropertyTriggerResource:
                    value2.ValueType = PropertyValueType.DataTriggerResource;
                    break;

                default:
                    throw new InvalidOperationException(SR.Get("UnexpectedValueTypeForDataTrigger", new object[] { value2.ValueType }));
            }
            this.PropertyValues[j] = value2;
        }
        base.Seal();
    }
}

void IAddChild.AddChild(object value)
{
    base.VerifyAccess();
    this.Setters.Add(Trigger.CheckChildIsSetter(value));
}

void IAddChild.AddText(string text)
{
    base.VerifyAccess();
    XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);
}

// Properties
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ConditionCollection Conditions
{
    get
    {
        base.VerifyAccess();
        return this._conditions;
    }
}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public SetterBaseCollection Setters
{
    get
    {
        base.VerifyAccess();
        if (this._setters == null)
        {
            this._setters = new SetterBaseCollection();
        }
        return this._setters;
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2024-11-23 08:49:44

基本上,事实并非如此。有一个内部 StyleHelper 类,它确定何时重新评估触发器。它根据触发器中的各种绑定来执行此操作。

具体可以看一下StyleHelper。采用 MultiDataTrigger 的 EvaluateOldNewStates 方法。

Basically, it doesn't. There is an internal StyleHelper class, which determines when to reevaluate the triggers. It does this based on the various bindings in the triggers.

Specifically, you can look at the StyleHelper. EvaluateOldNewStates method that takes a MultiDataTrigger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文