First I have the following code:
<TextBox Text="{Binding LengthUnit, Mode=OneWay}" IsReadOnly="True" Background="{x:Static SystemColors.ControlBrush}" />
I know that 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>
So, I can write:
<TextBox Text="{Binding LengthUnit, Mode=OneWay}" Style="{StaticResource readOnlyTextBox}" />
Since this text field is read-only, the binding mode cannot be twoway. So, is it possible to bind OneWay as the default for my TextBox with this style?
EDIT: I need to change the binding mode to OneWay because my property has only get-only, and not because I marked the TextBox as read-only. However, I still want to change the default binding mode of the text box to OneWay, if possible.
this is the code that i follow your suggestion but it doesn't work. Did I miss something?
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; } }
newman
source share