从 XAML 间接访问 属性 - WPF
Access Indirect property from XAML - WPF
我有一个名为 MyControl 的控件 class,它有直接布尔值 属性 AccessDirectProperty 和一个对象我的控制设置
public class MyControl : Control
{
public bool AccessDirectProperty
{
get; set;
}
public MyControlSettings ControlSettings
{
get;set;
}
}
请查找 MyControlSettings class 详细信息
public class MyControlSettings
{
public bool AccessIndirectProperty
{
get;set;
}
}
Direct 属性 AccessDirectProperty 可以从 XAML 无任何错误地访问。
<Window>
<Grid>
<local:MyControl AccessDirectProperty="True"/>
</Grid>
</Window>
但我无法从 XAML 中的对象 ControlSettings 访问 属性 AccessIndirectProperty。下面的代码无法做到这一点。
<Window>
<Grid>
<local:MyControl AccessDirectProperty="True" ControlSettings.AccessIndirectProperty=""/>
</Grid>
</Window>
谁能帮我解决这个问题?
恐怕 XAML 不支持访问 "nested" 属性。
但是,您可以使 ControlSettings
成为一个独立的 class attached properties:
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);
}
}
然后,
<local:MyControl x:Name="myControl"
AccessDirectProperty="True"
ControlSettings.AccessIndirectProperty="True" />
将设置一个可以通过
访问的值
var p = ControlSettings.GetAccessIndirectProperty(myControl); // yields True
现在,在技术层面上,以下内容对于修改通过 [=34 提供的现有 MyControlSettings 实例的 属性 没有用=]。但是,如果您的用例允许创建一个全新的 MyControlSettings 实例并将其分配给 MyControl.ControlSettings,您可以在 XAML:
<local:MyControl>
<ControlSettings>
<local:MyControlSettings AccessIndirectProperty="true" />
</ControlSettings>
</local:MyControl>
旁注:术语“ControlSettings”向我建议,您想在某种 我的控制设置 "container"。现在,我不知道这样做的原因和动机是什么,但请记住,选择这种方法会使以有意义的方式使用数据绑定变得非常困难甚至不可能,因为这样的设置 属性 是应该是绑定目标。如果您希望能够使用单独的设置作为绑定目标(如 AccessIndirectProperty="{Binding Path=Source}"
),我宁愿建议您的 MyControl 将这些设置单独公开为 DependencyProperties。
我有一个名为 MyControl 的控件 class,它有直接布尔值 属性 AccessDirectProperty 和一个对象我的控制设置
public class MyControl : Control
{
public bool AccessDirectProperty
{
get; set;
}
public MyControlSettings ControlSettings
{
get;set;
}
}
请查找 MyControlSettings class 详细信息
public class MyControlSettings
{
public bool AccessIndirectProperty
{
get;set;
}
}
Direct 属性 AccessDirectProperty 可以从 XAML 无任何错误地访问。
<Window>
<Grid>
<local:MyControl AccessDirectProperty="True"/>
</Grid>
</Window>
但我无法从 XAML 中的对象 ControlSettings 访问 属性 AccessIndirectProperty。下面的代码无法做到这一点。
<Window>
<Grid>
<local:MyControl AccessDirectProperty="True" ControlSettings.AccessIndirectProperty=""/>
</Grid>
</Window>
谁能帮我解决这个问题?
恐怕 XAML 不支持访问 "nested" 属性。
但是,您可以使 ControlSettings
成为一个独立的 class attached properties:
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);
}
}
然后,
<local:MyControl x:Name="myControl"
AccessDirectProperty="True"
ControlSettings.AccessIndirectProperty="True" />
将设置一个可以通过
访问的值var p = ControlSettings.GetAccessIndirectProperty(myControl); // yields True
现在,在技术层面上,以下内容对于修改通过 [=34 提供的现有 MyControlSettings 实例的 属性 没有用=]。但是,如果您的用例允许创建一个全新的 MyControlSettings 实例并将其分配给 MyControl.ControlSettings,您可以在 XAML:
<local:MyControl>
<ControlSettings>
<local:MyControlSettings AccessIndirectProperty="true" />
</ControlSettings>
</local:MyControl>
旁注:术语“ControlSettings”向我建议,您想在某种 我的控制设置 "container"。现在,我不知道这样做的原因和动机是什么,但请记住,选择这种方法会使以有意义的方式使用数据绑定变得非常困难甚至不可能,因为这样的设置 属性 是应该是绑定目标。如果您希望能够使用单独的设置作为绑定目标(如 AccessIndirectProperty="{Binding Path=Source}"
),我宁愿建议您的 MyControl 将这些设置单独公开为 DependencyProperties。