簡體   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