Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make value of a custom control editable upon receiving focus
    primarykey
    data
    text
    <p>I created a custom WPF control which displays the value of the <code>Weight</code> property which is an int. When the user clicks on the custom control or when the control gets the focus, the user should be able to edit the value of the Weight property.</p> <p>To accomplish this, I tried the following:</p> <ul> <li>If the control is "inactive", the Weight property will be displayed in a TextBlock.</li> <li><p>If the control gets the focus, the Weight property will be displayed in a TextBox.</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfCustomControlLibrary1"&gt; &lt;Style TargetType="{x:Type local:CustomControl1}"&gt; &lt;Style.Resources&gt; &lt;ControlTemplate x:Key="Control_Focused" &gt; &lt;Border Background="Green" BorderBrush="Black" CornerRadius="50" Width="40" Height="40"&gt; &lt;StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"&gt; &lt;TextBox Width="35" Text="{Binding Mode=TwoWay,Path=Model.Weight,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:CustomControl1}} ,Converter={x:Static local:CustomControl1.IntToStringConverter}}" /&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;ControlTemplate x:Key="Control"&gt; &lt;Border Background="Green" BorderBrush="Black" CornerRadius="50" Width="40" Height="40"&gt; &lt;StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"&gt; &lt;TextBlock x:Name="PART_TextBlockWeighted" Text="{Binding Mode=TwoWay,Path=Model.Weight,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:CustomControl1}}}" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Style.Resources&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsFocused" Value="True"&gt; &lt;Setter Property="Template" Value="{StaticResource Control_Focused}" &gt;&lt;/Setter&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsFocused" Value="False"&gt; &lt;Setter Property="Template" Value="{StaticResource Control}" &gt;&lt;/Setter&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p></p></li> </ul> <p>The problem with this custom control style is that when the user clicks into the TextBox, the trigger will change the control template to the TextBlock. How can I fix this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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