3

我的控件有 5 个依赖属性。我像这样在 XAML 中设置它们:

<MyControl Prop1="1" Prop2="2" Prop3="3" Prop4="4" />

如您所见,我只设置了 XAML 中 5 个属性中的 4 个。

我需要找到一些机制来表明 XAML 中设置的所有属性都已被处理。通过这个事件,我可以运行我的 SetItAllUp() 方法。

选项 1。使用 DP 设定器

失败:不是一个选项,因为我只能调用一次 SetItAllUp()。这也具有基于 XAML 中每个 DP 的序号声明激活的副作用。如果我的属性之间存在某种类型的链接或依赖关系,这会破坏它。

选项 2。使用 DP 设置器,并测试所有值是否已设置

FAIL:不是一个选项,因为有时某些 DP 值是可选的 - 让我们假设确定可选值是否正确设置所需的逻辑太复杂,暂时无法实施此解决方案。

选项 3。使用 MyControl.Loaded

FAIL:不是一个选项,因为它触发得太早了。事实上,我能看到的每一个事件都过早地触发了。就好像对象被创建了,然后引擎盖下的东西开始根据声明设置 DP 值。


更新!加载是解决方案。我的问题有缺陷。

有什么活动什么的,对吧?// 谢谢

4

1 回答 1

2

加载工作正常。对困惑感到抱歉。

我测试了这个类:

public class MyPath : Path
{
    public MyPath()
    {
        Loaded += MyPath_Loaded;
    }

    void MyPath_Loaded(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Loaded");
    }
    public int Test1
    {
        get { return (int)GetValue(Test1Property); }
        set
        {
            SetValue(Test1Property, value);
            System.Diagnostics.Debug.WriteLine("Test1");
        }
    }
    public static readonly DependencyProperty Test1Property =
        DependencyProperty.Register("Test1", typeof(int), typeof(MyPath),
        new PropertyMetadata(DependencyProperty.UnsetValue, null));

    public int Test2
    {
        get { return (int)GetValue(Test2Property); }
        set
        {
            SetValue(Test2Property, value);
            System.Diagnostics.Debug.WriteLine("Test2");
        }
    }
    public static readonly DependencyProperty Test2Property =
        DependencyProperty.Register("Test2", typeof(int), typeof(MyPath),
        new PropertyMetadata(DependencyProperty.UnsetValue, null));

    public int Test3
    {
        get { return (int)GetValue(Test3Property); }
        set
        {
            SetValue(Test3Property, value);
            System.Diagnostics.Debug.WriteLine("Test3");
        }
    }
    public static readonly DependencyProperty Test3Property =
        DependencyProperty.Register("Test3", typeof(int), typeof(MyPath),
        new PropertyMetadata(DependencyProperty.UnsetValue, null));

    public int Test4
    {
        get { return (int)GetValue(Test4Property); }
        set
        {
            SetValue(Test4Property, value);
            System.Diagnostics.Debug.WriteLine("Test4");
        }
    }
    public static readonly DependencyProperty Test4Property =
        DependencyProperty.Register("Test4", typeof(int), typeof(MyPath),
        new PropertyMetadata(DependencyProperty.UnsetValue, null));

    public int Test5
    {
        get { return (int)GetValue(Test5Property); }
        set
        {
            SetValue(Test5Property, value);
            System.Diagnostics.Debug.WriteLine("Test5");
        }
    }
    public static readonly DependencyProperty Test5Property =
        DependencyProperty.Register("Test5", typeof(int), typeof(MyPath),
        new PropertyMetadata(DependencyProperty.UnsetValue, null));
}

使用此 XAML:

<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
    <local:MyPath Test1="1" Test2="2" Test3="3" Test4="4" />
</Grid>

并得到了这个跟踪:

Test1
Test2
Test3
Test4
Loaded

事实证明,Loaded 工作得很好。我之前的测试一定有其他因素。我的简化测试显示 Loaded 看起来很完美。

于 2012-06-01T00:43:48.357 回答