可以使用XAML来设置控件的嵌套属性(属性的属性)吗? [英] Can I use XAML to set a nested property (property of the value of a property) of a control?

查看:149
本文介绍了可以使用XAML来设置控件的嵌套属性(属性的属性)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF控件,它暴露了一个孩子(从它的ControlTemplate)通过只读属性。目前它只是一个CLR属性,但我不认为这有什么区别。



我想要能够设置子控件上的一个属性从我实例化主控制的XAML。 (实际上,我想约束它,但我认为设置它将是一个很好的第一步。)



这里有一些代码:

  public class ChartControl:Control 
{
public IAxis XAxis {get;私人集合

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

this.XAxis = GetTemplateChild(PART_XAxis)作为IAxis;
}
}

public interface IAxis
{
//这是我要设置的属性
double最大{get;组; }
$ b public class Axis:FrameworkElement,IAxis
{
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(Maximum,typeof(double),typeof (Axis),新的FrameworkPropertyMetadata(20.0,FrameworkPropertyMetadataOptions.AffectsRender,OnAxisPropertyChanged));

public double最大
{
get {return(double)GetValue(MaximumProperty); }
set {SetValue(MaximumProperty,value); }
}
}

这里有两种方法可以设置XAML中的嵌套属性(无法编译):

 <! -  
这不起作用:
属性XAxis.Maximum不存在于XML命名空间http://schemas.microsoft.com/winfx/2006/xaml/presentation中。
在XAxis类型中找不到attachable属性Maximum。
- >
< local:ChartControl XAxis.Maximum ={Binding Maximum}/>

<! -
这不起作用:
无法在属性元素上设置属性。
- >
< local:ChartControl>
< local:ChartControl.XAxis Maximum ={Binding Maximum}/>
< / local:ChartControl>

这是否可以?



没有我想我只需要在受控于孩子(在模板中)的主要控件上公开DP。不是那么糟糕,我猜,但我只是试图避免在主要控制下的财产爆炸。



干杯。

解决方案

您不能这样做...您可以通过其绑定中的路径访问嵌套属性,但不能在定义属性的值时。 / p>

你必须这样做:

 < local: ChartControl> 
< local:ChartControl.XAxis>
< local:Axis Maximum ={Binding Maximum}/>
< / local:ChartControl.XAxis>
< / local:ChartControl>


I've got a WPF Control that exposes one of it's children (from it's ControlTemplate) through a read-only property. At the moment it's just a CLR property, but I don't think that makes any difference.

I want to be able to set one of the properties on the child control from the XAML where I'm instantiating the main control. (Actually, I would like to bind to it, but I think setting it would be a good first step.)

Here's some code:

public class ChartControl : Control
{
    public IAxis XAxis { get; private set; }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        this.XAxis = GetTemplateChild("PART_XAxis") as IAxis;
    }
}

public interface IAxis
{
    // This is the property I want to set
    double Maximum { get; set; }
}

public class Axis : FrameworkElement, IAxis
{
    public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(Axis), new FrameworkPropertyMetadata(20.0, FrameworkPropertyMetadataOptions.AffectsRender, OnAxisPropertyChanged));

    public double Maximum
    {
        get { return (double)GetValue(MaximumProperty); }
        set { SetValue(MaximumProperty, value); }
    }
}

Here's the two ways I can think of setting the nested property in XAML (neither compile):

<!-- 
    This doesn't work:
    "The property 'XAxis.Maximum' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'."
    "The attachable property 'Maximum' was not found in type 'XAxis'."
-->
<local:ChartControl XAxis.Maximum="{Binding Maximum}"/>

<!-- 
    This doesn't work: 
    "Cannot set properties on property elements."
-->
<local:ChartControl>
    <local:ChartControl.XAxis Maximum="{Binding Maximum}"/>
</local:ChartControl>

Is this even possible?

Without it I guess I'll just need to expose DP's on the main control that get bound through to the children (in the template). Not so bad, I guess, but I was just trying to avoid an explosion of properties on the main control.

Cheers.

解决方案

You can't do it like this... you can access nested properties through its path in a binding, but not when you define the value of the property.

You have to do something like that :

<local:ChartControl>
    <local:ChartControl.XAxis>
        <local:Axis Maximum="{Binding Maximum}"/>
    </local:ChartControl.XAxis>
</local:ChartControl>

这篇关于可以使用XAML来设置控件的嵌套属性(属性的属性)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆