I have a Control class named MyControl and it has direct bool property AccessDirectProperty and an Object MyControlSettings
public class MyControl : Control
{
public bool AccessDirectProperty
{
get; set;
}
public MyControlSettings ControlSettings
{
get;set;
}
}
Please find the MyControlSettings class details
public class MyControlSettings
{
public bool AccessIndirectProperty
{
get;set;
}
}
Direct property AccessDirectProperty can be accessible from XAML without any error.
<Window>
<Grid>
<local:MyControl AccessDirectProperty="True"/>
</Grid>
</Window>
But I cannot access the property AccessIndirectProperty from the object ControlSettings in XAML. 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.
You could, however, make ControlSettings
an independent class with 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);
}
}
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 . However, if your use case allows creating and assigning an entirely new MyControlSettings instance to MyControl.ControlSettings , you can do so in 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". 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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.