繁体   English   中英

自定义控件派生自具有内部控件和属性的控件

[英]Custom control derived from control with controls inside and properties

我以这个控件为例:

泛型

<Style TargetType="{x:Type local:EditLabel}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:EditLabel}" >
                <StackPanel Name ="PART_SPPrincipal" VerticalAlignment="Center" Orientation="Horizontal"  >
                    <TextBox Name="PART_TextBox" Width="100" HorizontalAlignment="Left" />
                    <Label Name="PART_Label" BorderBrush="Black" BorderThickness="1"
                           HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

EditLabel.cs

public class EditLabel : UserControl
{

    static EditLabel()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(EditLabel), new FrameworkPropertyMetadata(typeof(EditLabel)));
    }


    private Label _label;

    public static readonly  DependencyProperty LabelWidthProperty = DependencyProperty.Register("LabelWidth", typeof(int), typeof(EditLabel),
        new FrameworkPropertyMetadata(10, FrameworkPropertyMetadataOptions.AffectsRender));



    [Description("Establece el ancho de la etiqueta de descripción"), Category("Lutx")]
    public int LabelWidth
    {
        get
        {
            return (int)GetValue(LabelWidthProperty);
        }

        set
        {
            SetValue(LabelWidthProperty, value);
            _label.Width = value;               
        }
    }
    public override void OnApplyTemplate()
    {
        //_txtBox = GetTemplateChild("PART_TextBox") as ButtonEdit;

        _label = GetTemplateChild("PART_Label") as Label;
    }
}    

问题是当我更改LabelWidth时什么也没有发生,我尝试过以下方法:
1)在设计时,当我在行上设置断点时,更改LabelWidth不会发生任何事情:
_label.Width = value;
从不停止执行时间

2)我的事也许是我尝试通过代码来完成的,并且在标签行的<Label Name="PART_Label" Width="{Binding LabelWith}"...尝试时: <Label Name="PART_Label" Width="{Binding LabelWith}"...
什么都没发生

3)当我将控件放在窗口中时

    <local:EditLabel  LabelWidth="30"/>

什么都没发生

目前我没有更多的主意,因此,非常感谢您的帮助

在LabelWidthProperty(DependencyProperty)的声明中,在PropertyMetadata中放置一个回调,并将_label.Width设置为e.NewValue 那应该解决您的问题。 删除_label.Width = value; LabelWidth

谢谢您的TYY,我无法投票给您,因为我没有15个声誉。

是的,似乎是正确的方法,但是问题是PropertyChangedCallback必须声明为静态,而不是实例的一部分,因此更改后请参见代码。 总是异常,因为_label为null。 查看新代码(还对OnLabelChanged注释行进行了测试):

公共类EditLabel:UserControl {

    static EditLabel()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(EditLabel), new FrameworkPropertyMetadata(typeof(EditLabel)));
    }


    private Label _label;

    public Label Label
    {
        get { return _label; }
        set { _label = value; }
    }

    public static readonly  DependencyProperty LabelWidthProperty = DependencyProperty.Register("LabelWidth", typeof(int), typeof(EditLabel), 
                                                                    new PropertyMetadata(10, new PropertyChangedCallback(OnLabelChanged)));



    [Description("Establece el ancho de la etiqueta de descripción"), Category("Lutx")]
    public int LabelWidth
    {
        get
        {
            return (int)GetValue(LabelWidthProperty);
        }

        set
        {
            SetValue(LabelWidthProperty, value);          
        }
    }


    private static void OnLabelChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
       EditLabel el  = obj as EditLabel;
       el.Label.Width = (int)e.NewValue;

       //Label lbl = el.GetTemplateChild("PART_Label") as Label;
       //lbl.Width(int)e.NewValue;          

     }

    public override void OnApplyTemplate()
    {
        //_txtBox = GetTemplateChild("PART_TextBox") as ButtonEdit;

        _label = GetTemplateChild("PART_Label") as Label;


        SetBinding(LabelWidthProperty, new Binding
                        {
                            Source = _label,
                            Path = new PropertyPath("Width")
                        });
    }
}    

暂无
暂无

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

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