【问题标题】:Set Binding property in Style in XAML C# [duplicate]在 XAML C# 中的样式中设置绑定属性 [重复]
【发布时间】:2017-06-28 12:13:00
【问题描述】:

我正在寻找一种在Style 中设置绑定属性UpdateSourceTrigger 的方法。目前我在每个TextBox 上手动设置它,如下所示:

<TextBox Text="{Binding boundValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

我想做的是这样的(但这显然行不通):

<Window.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Binding.UpdateSourceTrigger" Value="PropertyChanged" />
    </Style>
</Window.Resources>
<Grid>
    <TextBox Text="{Binding boundValue, Mode=TwoWay}"/>
</Grid>

【问题讨论】:

    标签: c# wpf xaml data-binding wpf-style


    【解决方案1】:

    样式适用于FrameworkElements(和派生控件)。

    Binding 不是 FrameworkElement。

    无论如何,您都可以创建自己的 markup extension 来设置您需要的 Binding 属性:

    public class PropertyChangedBinding : Binding
    {
        public PropertyChangedBinding(string path)
            : base(path)
        {
            UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged;
        }
    
        public PropertyChangedBinding()
            : base()
        {
            UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged;
        }
    }
    

    所以在你的 XAML 中你可以使用{local:PropertyChangedBinding ...}

    【讨论】:

    • 效果很好。谢谢!
    • 不客气@RomanoZumbé
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多