【问题标题】:Enum dependency property is set to null when using a WPF style property setter in Metro在 Metro 中使用 WPF 样式属性设置器时,枚举依赖属性设置为 null
【发布时间】:2012-09-05 14:10:53
【问题描述】:

在我们的 Metro 应用程序中,我有一个 enum 类型的附加属性。

当直接将元素上的属性设置为 XAML 属性时,该值设置得很好,但是当在样式中使用 Setter 元素时,虽然设置了属性,但它被设置为 null 值(即,e.NewValue在下面的代码中为空)

这是为什么?它引起了问题,显然不能转换为枚举类型。谢谢。

以下是相关代码:

    public static readonly DependencyProperty KeyboardScrollRestrictionStyleProperty =
        DependencyProperty.RegisterAttached("KeyboardScrollRestrictionStyle", typeof(KeyboardScrollRestrictionStyle), typeof(FlipViewScrollBehaviour),
            new PropertyMetadata(KeyboardScrollRestrictionStyle.TextBox, OnKeyboardScrollRestrictionStyleChanged));

    static void OnKeyboardScrollRestrictionStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    { 
       //I check e.NewValue at breakpoint
    }

这可行(e.NewValue 是 TextBox):

<TemplatedControls:WatermarkTextBox       
Behaviours:FlipViewScrollBehaviour.KeyboardScrollRestrictionStyle="TextBox"
/>

这不是(e.NewValue 为空):

<Style x:Key="TimesheetLineListViewItemTextBox" TargetType="TextBox">
    <Setter Property="Behaviours:FlipViewScrollBehaviour.KeyboardScrollRestrictionStyle" Value="TextBox" />
</Style>

【问题讨论】:

    标签: c# wpf xaml microsoft-metro dependency-properties


    【解决方案1】:

    如果您在本地设置 textBox 中的附加属性,然后尝试使用您的样式中的 Setter 覆盖它,这是我能猜到的唯一情况,这会阻止它工作。例如 -

    <TemplatedControls:WatermarkTextBox    
    Behaviours:FlipViewScrollBehaviour.KeyboardScrollRestrictionStyle="TextBox"
    Style ="{StaticResource TimesheetLineListViewItemTextBox}"/>
    

    在这里你是在本地设置它,如果尝试在样式设置器中设置,它将不起作用。参考这个链接-Dependency Property Precedence Order

    【讨论】:

    • 感谢您的回答。遗憾的是,这不是问题,因为使用此样式的所有控件都没有明确设置,但问题仍然存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    相关资源
    最近更新 更多