Search code examples
c#wpfxaml

WPF changing textblock to a combo box on focus and back to textblock after losing focus


I'm not sure if its even possible but I've been searching the internet and cant find anything even remotely to what I am want. I have done similar thing to textbox to turn it into textblock when not focused in order to use the TextTrimming="CharacterEllipsis" property not available to textbox. And on a datagrid there is a similar action to what I want. The DataGrid has DataGridTemplateColumn which has access to both CellTemplate and CellEditingTemplate so for a datagrid I was able to have a textblock that changes to combobox when editing. The issue is I dont want a DataGrid and I am unable to use the same method used for textbox because ig textblock doesnt have access to the property "Control.Template" that I used in the style data trigger for textbox.

Here is example code of the textbox style I used:

<MultiDataTrigger>
  <MultiDataTrigger.Conditions>
    <Condition Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="false"/>
    <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="true"/>
  </MultiDataTrigger.Conditions>
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate TargetType="TextBox">
        <TextBlock Text="{TemplateBinding Text}" TextTrimming="CharacterEllipsis"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</MultiDataTrigger>

Ive tried variations of this just to get it to work messing around with different properties but my knowledge of how triggers work is limited so it was more just guessing and checking of different things getting nowhere so I decided to come here for some assistance.

I need to use textblock because of the texttrimming attribute yet again but at this rate i make it a textbox and give it a style to turn into combobox when focused and a textblock when unfocused if thats possible but seems silly.


Solution

  • I ended up changing the TextBlock to a ComboBox and doing what I was trying to but changing the ComboBox to TextBlock when not focused. I did this because I don't believe TextBlock has the "Control.Template" Property but even if it did I had other focusing issues regarding the TextBlock anyways so this way works much better.

    <Style x:Key="ComboBoxTest" TargetType="ComboBox">
      <Style.Triggers>
        <MultiDataTrigger>
          <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}}" Value="false"/>
            <Condition Binding="{Binding IsFocused, RelativeSource={RelativeSource Self}}" Value="false"/>
          </MultiDataTrigger.Conditions>
          <MultiDataTrigger.Setters>
            <Setter Property="Control.Template">
              <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                  <Border Background="{Binding Colour}" Width="{Binding Width}" BorderBrush="Transparent" BorderThickness="1" AllowDrop="True">
                    <TextBlock Padding="5,0,5,0" Text="{Binding Text}" ToolTip="{Binding Text}" TextTrimming="CharacterEllipsis" VerticalAlignment="Center" Focusable="False"/>
                  </Border>
                </ControlTemplate>
              </Setter.Value>
            </Setter>
          </MultiDataTrigger.Setters>
        </MultiDataTrigger>
      </Style.Triggers>
    </Style>
    

    Hope this helps anyone else trying to achieve similar!