简体   繁体   English

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

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

I have this control as an example: 我以这个控件为例:

Generic.xaml 泛型

<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 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;
    }
}    

The problem is nothing happens when I change LabelWidth, I've tried these ways: 问题是当我更改LabelWidth时什么也没有发生,我尝试过以下方法:
1) In design time when I change LabelWidth nothing happens, if I put a breakpoint at line: 1)在设计时,当我在行上设置断点时,更改LabelWidth不会发生任何事情:
_label.Width = value;
never stop there in execution time 从不停止执行时间

2)I thing maybe the problem is i try to do it from code, and when tried in Generic.xaml in Label line: <Label Name="PART_Label" Width="{Binding LabelWith}"... 2)我的事也许是我尝试通过代码来完成的,并且在标签行的<Label Name="PART_Label" Width="{Binding LabelWith}"...尝试时: <Label Name="PART_Label" Width="{Binding LabelWith}"...
nothing happens 什么都没发生

3) When i place the control in a window 3)当我将控件放在窗口中时

    <local:EditLabel  LabelWidth="30"/>

nothing happens 什么都没发生

I've not more ideas at this time so your help will be really appreciated 目前我没有更多的主意,因此,非常感谢您的帮助

In the declaration of your LabelWidthProperty (DependencyProperty) put a callback in your PropertyMetadata and set the _label.Width to e.NewValue . 在LabelWidthProperty(DependencyProperty)的声明中,在PropertyMetadata中放置一个回调,并将_label.Width设置为e.NewValue That should fix your problem. 那应该解决您的问题。 Remove the _label.Width = value; 删除_label.Width = value; from LabelWidth LabelWidth

Thank's TYY I cannot vote up you because i have not 15 reputation. 谢谢您的TYY,我无法投票给您,因为我没有15个声誉。

Yes seems the right way but the problem is PropertyChangedCallback must be declared as static and not is an instance's part, so after changes see the code. 是的,似乎是正确的方法,但是问题是PropertyChangedCallback必须声明为静态,而不是实例的一部分,因此更改后请参见代码。 Always exception because _label is null. 总是异常,因为_label为null。 See the new code (OnLabelChanged commented lines also tested): 查看新代码(还对OnLabelChanged注释行进行了测试):

public class EditLabel : UserControl { 公共类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.

相关问题 从Label:派生的WPF控件是自定义属性(使用DP)不使用setter? - WPF control derived from Label: are custom properties (with DP) not using the setter? 自定义控件内的控件不可移动 - Controls inside custom control aren't movable 如何在自定义控件中访问内部控件模板中的控件 - How to access controls in inside Control Template in a Custom control 在控件内部向控件添加控件 - Adding Controls to Control inside of a Control 将itemssource交给从datagrid派生的自定义控件 - Hand itemssource to custom control derived from datagrid 从组件派生的自定义控件-OnCreate事件? - Custom control derived from Component - OnCreate event? 子控件在从另一个控件派生的自定义控件中不可见 - Subcontrols not visible in custom control derived from another control 获取控件内部的控件 - Get Controls inside a Control 如何处理来自用户选项卡中的控件的事件,该用户控件是从选项卡页派生的,该选项卡在运行时动态添加到选项卡控件? - How do I handle events from controls inside a User Control derived from a Tab Page meant to be added dynamically to a Tab Control at run time? 用户控件以及其他控件作为属性 - User control with other controls as properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM