我以这个控件为例:
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"/>什么都没发生
我现在没有更多的想法,所以你的帮助将是非常感谢的
发布于 2014-04-25 12:28:14
在LabelWidthProperty (DependencyProperty)的声明中,在PropertyMetadata中放一个回调,并将_label.Width设置为e.NewValue。这应该能解决你的问题。从_label.Width = value;中删除LabelWidth
发布于 2014-04-28 07:58:19
谢谢你,我不能投你的票,因为我没有15个名誉。
是的似乎是正确的方法,但问题是必须将PropertyChangedCallback声明为静态的,而不是实例的一部分,因此在更改之后请参阅代码。总是异常,因为_label是空的。请参阅新代码(OnLabelChanged注释行也测试过):
public class 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")
});
}
} https://stackoverflow.com/questions/23292625
复制相似问题