WPF style setter not working

后端 未结 1 722
[愿得一人]
[愿得一人] 2021-01-23 02:55

I have a custom user control that contains a combo box. I\'ve added a ComboBoxWidth dependency property to allow developer to set width if they want. Using a style setter, I\'

相关标签:
1条回答
  • 2021-01-23 03:08

    A so-called "local value" of a dependency property, like

    this.ComboBoxWidth = 175.0;
    

    has higher value precedence than a value from a Style Setter, like

    <Setter Property="ComboBoxWidth" Value="400"/>
    

    Hence the Style Setter has no effect.

    You should assign a new default value by overriding dependency property metadata:

    public class AngleUserControl : ...
    {
        static AngleUserControl()
        {
            ComboBoxWidthProperty.OverrideMetadata(
                typeof(AngleUserControl),
                new PropertyMetadata(175d));
        }
    }
    

    See Dependency Property Value Precedence for reference.

    0 讨论(0)
提交回复
热议问题