问题
I would love to understand a special part about WPF. I am setting a GridLength (width) value for a ColumnDefinition inside a Control. The value is referencing a static Property of a static class. Of course it does not support INotifyPropertyChanged but all I want is to update the value and when the next instance of this Control gets created it should take the new value. But it doesn't work this way.
I experienced the same behaviour when binding to a dependency property that gets its default value via a static Property. This is never evaluated again and that is logical to me. But just using a field should be ok when creating the next instance.
One thing I just realize while writing this. I am talking about a custom control and the value is used within a Template. So I am assuming that the ElementFactory gets created using that value and then just pushes out little controls with its internal values. Can somebody confirm that?
If that's how it works, what would be a workaround or better: the correct way to handle this? I am applying a new value to a control-resource in OnApplyTemplate currently.
Thanks for you thoughts on this. Best regards!
EDIT: The code is pretty straight forward:
// static values
public static class DesignDefaultValues
{
static DesignDefaultValues()
{
LabelColGridLength = new GridLength(22d);
}
public static GridLength LabelColGridLength { get; private set; }
}
// XAML USAGE
<Style TargetType="{x:Type local:SomeClass}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SomeClass}">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{x:Static local:DesignDefaultValue.LabelColGridLength }" />
<ColumnDefinition Width="*" /> ...
来源:https://stackoverflow.com/questions/21824046/xstatic-value-in-control-does-not-update