绑定 WPF DependencyProperty 的默认设置

发布于 2024-08-21 16:05:26 字数 499 浏览 1 评论 0原文

我创建了一个名为 MyCustomComboBox 的自定义用户控件。在应用程序的每个地方,我都会执行以下操作:

    <Widgets:MyCustomComboBox
        Foo="{Binding Foo, 
            UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> 

MyCustomComboxBox 具有依赖属性 Foo,我在组合框中有一些验证和其他逻辑,这就是我将其包装在自定义控件中的原因。

自定义组合框包含在另一个用户控件中,该控件也具有组合框绑定到的 Foo 属性。

但我还必须设置 UpdateSourceTriggerMode,我想以某种方式指定这些是绑定到该 DependencyProperty 时的默认值。能做到吗?

I've created a custom user control named MyCustomComboBox. Everywhere in the application I put it I do the following:

    <Widgets:MyCustomComboBox
        Foo="{Binding Foo, 
            UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> 

MyCustomComboxBox has the dependency property Foo, I have some validation and other logic in the combobox which is the very reason why I wrapped it up in a custom control.

The custom combobox is included another user control which also has a Foo property, which the combobox's is bound to.

But I also have to set UpdateSourceTrigger and Mode, I would like to somehow specify that those are the default values when binding to that DependencyProperty. Can it be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年九夏 2024-08-28 16:05:26

可以在依赖项属性元数据中指定默认的 BindingMode:

public static readonly DependencyProperty FooProperty = DependencyProperty.Register(
    "Foo",
    typeof(string),
    typeof(MyCustomComboBox),
    new FrameworkPropertyMetadata(
        null,
        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);

但是,据我所知,无法为更新源触发器提供默认值。

The default BindingMode can be specified in the dependency property metadata:

public static readonly DependencyProperty FooProperty = DependencyProperty.Register(
    "Foo",
    typeof(string),
    typeof(MyCustomComboBox),
    new FrameworkPropertyMetadata(
        null,
        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);

However, to my knowledge there is no way to provide a default for the update source trigger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文