Formatting issues trying to create a WPF shortcut template that allows you to select text

I have a requirement to allow the selection of text displayed on read-only screens.

The simple solution one of our developers came up with uses TextBoxLabel or TextBlock instead with the following style:

<Style x:Key="ControlData" TargetType="{x:Type TextBox}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="IsReadOnly" Value="True" />
    <Setter Property="TextWrapping" Value="Wrap" />
    <!-- unrelated properties ommitted -->
</Style>

I don't like the idea of ​​using a TextBox there, by the way, because it forces me to use Binding Mode=OneWayread-only properties, so I tried to define a style that I can apply to the label to get the same result:

<Style x:Key="SelectableLabel" TargetType="Label">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Label">
                <TextBox Style="{StaticResource ControlData}"
                         Text="{Binding Path=Content, Mode=OneWay,
                                RelativeSource={RelativeSource FindAncestor,
                                                AncestorType=Label}}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The problem is that some of my bindings have StringFormatset, and this is lost.

  • Is there a way to keep the formatting of external binding?
  • Should I create my template / binding differently?
  • , ?
  • nitpicking TextBox?
+3
2

, . WPF, , xaml.

TemplateBinding instad of Normal Binding? , , DataTemplates.

ContentPresenter, , , Text string...

Edit: , , ...

:

<Application.Resources>
    <LabelTest:ContentToTextConverter x:Key="contentConverter"  />
    <Style TargetType="Label">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <TextBox DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource FindAncestor,AncestorType=Label}}">
                        <TextBox.Text>
                            <MultiBinding Converter="{StaticResource contentConverter}" >
                                <Binding Mode="OneWay" Path="ContentStringFormat" />
                                <Binding Mode="OneWay" Path="Content" />
                            </MultiBinding>
                        </TextBox.Text>
                    </TextBox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

ContentToTextConverter :

public class ContentToTextConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var format = values[0] as string;

        if (string.IsNullOrEmpty(format)) format = "{0}";
        else if(format.IndexOf('{') < 0) format = "{0:" + format + "}";

        return string.Format(culture, format, values[1]);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

:

<Label ContentStringFormat="{}{0}$">
     <System:Int64>15</System:Int64>
</Label>

, , .

+2

nitpicking TextBox?

.

+2

All Articles