Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF TextBox: How to change binding mode default to OneWay?
    text
    copied!<p>Initially, I have the following code:</p> <pre><code>&lt;TextBox Text="{Binding LengthUnit, Mode=OneWay}" IsReadOnly="True" Background="{x:Static SystemColors.ControlBrush}" /&gt; </code></pre> <p>I know I can define a style like this:</p> <pre><code>&lt;Style TargetType="{x:Type TextBox}" x:Key="readOnlyTextBox"&gt; &lt;Setter Property="Background" Value="{x:Static SystemColors.ControlBrush}"&gt;&lt;/Setter&gt; &lt;Setter Property="IsReadOnly" Value="True"&gt;&lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>So that I can write:</p> <pre><code>&lt;TextBox Text="{Binding LengthUnit, Mode=OneWay}" Style="{StaticResource readOnlyTextBox}" /&gt; </code></pre> <p>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?</p> <p>EDIT: 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. </p> <hr> <p>this is the code I have following your suggestion, but it doesn't work. Did I miss anything?</p> <pre><code>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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload