首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义控件派生自定义控件,其中包含控件和属性。

自定义控件派生自定义控件,其中包含控件和属性。
EN

Stack Overflow用户
提问于 2014-04-25 12:05:07
回答 2查看 57关注 0票数 0

我以这个控件为例:

Generic.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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)我想--也许问题是,我试图用代码来做这件事,当在Generic.xaml中使用标签行:<Label Name="PART_Label" Width="{Binding LabelWith}"...

什么都没发生

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

代码语言:javascript
复制
    <local:EditLabel  LabelWidth="30"/>

什么都没发生

我现在没有更多的想法,所以你的帮助将是非常感谢的

EN

回答 2

Stack Overflow用户

发布于 2014-04-25 12:28:14

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

票数 0
EN

Stack Overflow用户

发布于 2014-04-28 07:58:19

谢谢你,我不能投你的票,因为我没有15个名誉。

是的似乎是正确的方法,但问题是必须将PropertyChangedCallback声明为静态的,而不是实例的一部分,因此在更改之后请参阅代码。总是异常,因为_label是空的。请参阅新代码(OnLabelChanged注释行也测试过):

代码语言:javascript
复制
    public class EditLabel : UserControl     {
代码语言:javascript
复制
    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")
                        });
    }
}    
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23292625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档