简体   繁体   English

从 XAML 访问间接属性 - WPF

[英]Access Indirect property from XAML - WPF

I have a Control class named MyControl and it has direct bool property AccessDirectProperty and an Object MyControlSettings我有一个名为MyControl的 Control 类,它具有直接布尔属性AccessDirectProperty和一个对象MyControlSettings

    public class MyControl : Control
    {
        public bool AccessDirectProperty
        {
            get; set;
        }
        public MyControlSettings ControlSettings
        {
            get;set;
        }
    }

Please find the MyControlSettings class details请找到MyControlSettings类的详细信息

public class MyControlSettings
{
    public bool AccessIndirectProperty
    {
        get;set;
    }
}

Direct property AccessDirectProperty can be accessible from XAML without any error.可以从 XAML 访问直接属性AccessDirectProperty ,而不会出现任何错误。

<Window>
    <Grid>
        <local:MyControl AccessDirectProperty="True"/>
    </Grid>
</Window>

But I cannot access the property AccessIndirectProperty from the object ControlSettings in XAML.但是我无法从 XAML 中的对象ControlSettings访问属性AccessIndirectProperty The below code fails to do that.下面的代码无法做到这一点。

<Window>
    <Grid>
        <local:MyControl AccessDirectProperty="True" ControlSettings.AccessIndirectProperty=""/>
    </Grid>
</Window>

Can anyone help me on this?谁可以帮我这个事?

I'm afraid that XAML does not support accessing "nested" properties.恐怕 XAML 不支持访问“嵌套”属性。

You could, however, make ControlSettings an independent class with attached properties :但是,您可以使ControlSettings成为具有附加属性的独立类:

public class ControlSettings : DependencyObject
{
    public static readonly DependencyProperty AccessIndirectPropertyProperty =
        DependencyProperty.RegisterAttached(
              "AccessIndirectProperty", typeof(bool), typeof(ControlSettings),
              new PropertyMetadata(false));

    public static bool GetAccessIndirectProperty(DependencyObject d)
    {
        return (bool) d.GetValue(AccessIndirectPropertyProperty);
    }
    public static void SetAccessIndirectProperty(DependencyObject d, bool value)
    {
        d.SetValue(AccessIndirectPropertyProperty, value);
    }
}

Then,然后,

<local:MyControl x:Name="myControl" 
                 AccessDirectProperty="True" 
                 ControlSettings.AccessIndirectProperty="True" />

would set a value which could be accessed via将设置一个可以通过访问的值

var p = ControlSettings.GetAccessIndirectProperty(myControl); // yields True

Now, on a technical level, the following is not useful to modify a property of an existing MyControlSettings instance provided through MyControl.ControlSettings .现在,在技术层面上,以下内容对于修改通过MyControl.ControlSettings提供的现有MyControlSettings实例的属性没有用。 However, if your use case allows creating and assigning an entirely new MyControlSettings instance to MyControl.ControlSettings , you can do so in XAML:但是,如果您的用例允许创建一个全新的MyControlSettings实例并将其分配给MyControl.ControlSettings ,则可以在 XAML 中执行此操作:

<local:MyControl>
    <ControlSettings>
        <local:MyControlSettings AccessIndirectProperty="true" />
    </ControlSettings>
</local:MyControl>

A side note: The term " ControlSettings " suggest to me that you want to kind of "package" control settings/properties in some kind of MyControlSettings "container".旁注:术语“ ControlSettings ”向我建议您希望在某种MyControlSettings “容器”中“打包”控制设置/属性。 Now, i don't know why and what the motivation for this is, but keep in mind that choosing this approach can make it very hard or even impossible to use data bindings in a meaningful way where such a settings property is supposed to be the binding target.现在,我不知道为什么以及这样做的动机是什么,但请记住,选择这种方法会使以有意义的方式使用数据绑定变得非常困难甚至不可能,因为这种设置属性应该是绑定目标。 If you want to be able to use individual settings as binding target (like AccessIndirectProperty="{Binding Path=Source}" ), i would rather suggest your MyControl exposes those settings individually as DependencyProperties .如果您希望能够使用单个设置作为绑定目标(如AccessIndirectProperty="{Binding Path=Source}" ),我宁愿建议您的MyControl将这些设置单独公开为DependencyProperties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM