我把这个控制作为一个例子:
的 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
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)当我将控件放在窗口中时
<local:EditLabel LabelWidth="30"/>
没有任何反应
此时我没有更多想法,所以我们非常感谢您的帮助
答案 0 :(得分:0)
在LabelWidthProperty(DependencyProperty)的声明中,在PropertyMetadata中添加一个回调,并将_label.Width
设置为e.NewValue
。这应该可以解决你的问题。从_label.Width = value;
LabelWidth
答案 1 :(得分:0)
感谢&t; s 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")
});
}
}