WPF文本框:如何将绑定模式默认更改为OneWay? [英] WPF TextBox: How to change binding mode default to OneWay?

查看:406
本文介绍了WPF文本框:如何将绑定模式默认更改为OneWay?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我有以下代码:

<TextBox Text="{Binding LengthUnit, Mode=OneWay}" IsReadOnly="True" Background="{x:Static SystemColors.ControlBrush}" />

我知道我可以定义这样的样式:

I know I can define a style like this:

<Style TargetType="{x:Type TextBox}" x:Key="readOnlyTextBox">
    <Setter Property="Background" Value="{x:Static SystemColors.ControlBrush}"></Setter>
    <Setter Property="IsReadOnly" Value="True"></Setter>
</Style>

这样我就可以写:

<TextBox Text="{Binding LengthUnit, Mode=OneWay}" Style="{StaticResource readOnlyTextBox}" />

由于此文本框为只读,因此绑定模式不能为双向.因此,是否可以使用这种样式将OneWay绑定作为我的TextBox的默认值?

Because this textbox is readonly, the binding mode cannot be twoway. So, is it possible to make the OneWay binding as the default for my TextBox with this style?

我需要将绑定模式更改为OneWay,因为我的属性是仅获取的,而不是因为我将TextBox标记为只读.但是,如果可能的话,我仍然想将文本框的默认绑定模式更改为OneWay.

I need to change the binding mode to OneWay, because my property is get-only, not because I marked the TextBox readonly. However, I still want to change the default binding mode of the textbox to OneWay if possible.

这是我遵循您的建议的代码,但是它不起作用.我有什么想念吗?

this is the code I have following your suggestion, but it doesn't work. Did I miss anything?

public class ReadOnlyTextBox : TextBox
{
    static ReadOnlyTextBox()
    {
        TextBox.TextProperty.OverrideMetadata(typeof(ReadOnlyTextBox), 
            new FrameworkPropertyMetadata() { BindsTwoWayByDefault = false, Journal = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.Explicit }); 
    }
    public ReadOnlyTextBox()
    {
        base.Background = SystemColors.ControlBrush;
        base.IsReadOnly = true;            
    }
}

推荐答案

由于此文本框为只读,因此绑定模式不能为双向.

Because this textbox is readonly, the binding mode cannot be twoway.

为什么不呢? IsReadOnly将阻止用户修改Text,从而修改属性.只需确保不要在代码中修改Text属性即可.

Why not? IsReadOnly will prevent the user from modifying the Text and thereby modifying the property. Just make sure not to modify the Text property in code.

如果您将TextBox子类化,则可以阻止bound属性更新.如果这样做,则可以覆盖TextBox.Text Dependency属性元数据.

You can prevent the bound property from updating if you subclass TextBox. If you do so, you can override the TextBox.Text Dependency Property metadata.

public class TextBoxEx : TextBox
{
    public TextBoxEx() : base() { }

    static TextBoxEx()
    {
        TextBox.TextProperty.OverrideMetadata(typeof(TextBoxEx), 
            new FrameworkPropertyMetadata() { BindsTwoWayByDefault = false, Journal = true,
                DefaultUpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.Explicit });
    }

}

对于某些情况,将BindsTwoWayByDefault更改为false对我不起作用,但是您可以将DefaultUpdateSourceTrigger设置为Explicit,这意味着除非通过代码进行操作,否则绑定属性将不会被更新,从而有效地使绑定成为OneWay.

For some reasion changing BindsTwoWayByDefault to false doesn't work for me, but you can set DefaultUpdateSourceTrigger to Explicit which means that the bound property won't be updated unless done so by code, effectively making the binding OneWay.

这篇关于WPF文本框:如何将绑定模式默认更改为OneWay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆