Frage

I have a control like below xaml with Read only enabled.

          <TextBox  Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  Background="Transparent" IsReadOnly="True" BorderThickness="0" TextWrapping="Wrap" >   

Now when i double click this text box , i should be able to enter a text. Readonly property should become false

If I move to another item in the window other than this text box , then the text box should become readonly again.

I am trying to do it with Triggers. but not getting the right hint . Can anyone help me here ?

War es hilfreich?

Lösung

You can make this with 2 events, MouseDoubleClick and LostFocus

<Grid>
    <TextBox IsReadOnly="True"
             MouseDoubleClick="TextBox_MouseDoubleClick"
             LostFocus="TextBox_LostFocus"/>
</Grid>

In you procedural code:

private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    TextBox textBox = sender as TextBox;
    textBox.IsReadOnly = false;
    //textBox.CaretIndex = textBox.Text.Count();
    textBox.SelectAll();
}

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    TextBox textBox = sender as TextBox;
    textBox.IsReadOnly = true;
}

Andere Tipps

You can use Style and EventTrigger to do that

<Window xmlns:sys="clr-namespace:System;assembly=mscorlib" ...>
    <Window.Resource>
        <Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="LostFocus">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Duration="0"
                                    Storyboard.TargetProperty="(TextBox.IsReadOnly)">
                                    <DiscreteObjectKeyFrame KeyTime="0">
                                        <DiscreteObjectKeyFrame.Value>
                                            <sys:Boolean>True</sys:Boolean>
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseDoubleClick">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Duration="0"
                                       Storyboard.TargetProperty="(TextBox.IsReadOnly)">
                                    <DiscreteObjectKeyFrame KeyTime="0">
                                        <DiscreteObjectKeyFrame.Value>
                                            <sys:Boolean>False</sys:Boolean>
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resource>
    ...
    <TextBox Style="{StaticResource MyTextBoxStyle}" .../>
</Window>

You can use System.Windows.Interactivity assembly (msdn) to do that.

First: create helper class to set properties:

public class SetterAction : TriggerAction<DependencyObject>
{
    public SetterAction()
    {
        Setters = new List<Setter>();
    }

    public List<Setter> Setters { get; set; }

    protected override void Invoke(object parameter)
    {
        foreach (var item in Setters)
        {
            AssociatedObject.SetValue(item.Property, item.Value);
        }
    }
}

XAML:

<TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
         Background="Transparent" IsReadOnly="True" BorderThickness="0" TextWrapping="Wrap"
         Height="30" Width="200">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:EventTrigger.Actions>
                <local:SetterAction>
                    <local:SetterAction.Setters>
                        <Setter Property="TextBox.IsReadOnly" Value="False" />
                        <Setter Property="TextBox.Background" Value="Green" />
                    </local:SetterAction.Setters>
                </local:SetterAction>
            </i:EventTrigger.Actions>
        </i:EventTrigger>
        <i:EventTrigger EventName="LostFocus">
            <i:EventTrigger.Actions>
                <local:SetterAction>
                    <local:SetterAction.Setters>
                        <Setter Property="TextBox.IsReadOnly" Value="True" />
                        <Setter Property="TextBox.Background" Value="Red" />
                    </local:SetterAction.Setters>
                </local:SetterAction>
            </i:EventTrigger.Actions>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

Where i is:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

The best answer is in the form of Attached Dependency Property. Usage:

xmlns:e="clr-namespace:Extensions"
<TextBox e:TextBoxExtensions.IsEditableOnlyOnDoubleClick="True"/>
#nullable enable

namespace Extensions;

public static class TextBoxExtensions
{
    #region IsEditableOnlyOnDoubleClick

    public static readonly DependencyProperty IsEditableOnlyOnDoubleClickProperty =
        DependencyProperty.RegisterAttached(
            nameof(IsEditableOnlyOnDoubleClickProperty).Replace("Property", string.Empty),
            typeof(bool),
            typeof(TextBoxExtensions),
            new PropertyMetadata(false, OnIsEditableOnlyOnDoubleClickChanged));

    [AttachedPropertyBrowsableForType(typeof(TextBox))]
    public static bool GetIsEditableOnlyOnDoubleClick(DependencyObject element)
    {
        return (bool)element.GetValue(IsEditableOnlyOnDoubleClickProperty);
    }

    public static void SetIsEditableOnlyOnDoubleClick(DependencyObject element, bool value)
    {
        element.SetValue(IsEditableOnlyOnDoubleClickProperty, value);
    }

    private static void OnIsEditableOnlyOnDoubleClickChanged(
        DependencyObject element,
        DependencyPropertyChangedEventArgs args)
    {
        if (element is not TextBox textBox)
        {
            throw new ArgumentException($"{nameof(element)} should be {nameof(TextBox)}.");
        }

        if (args.OldValue is true)
        {
            textBox.MouseDoubleClick -= TextBox_MouseDoubleClick;
            textBox.LostFocus -= TextBox_LostFocus;
            textBox.IsReadOnly = false;
        }

        if (args.NewValue is true)
        {
            textBox.MouseDoubleClick += TextBox_MouseDoubleClick;
            textBox.LostFocus += TextBox_LostFocus;
            textBox.IsReadOnly = true;
        }
    }

    private static void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (sender is not TextBox textBox)
        {
            return;
        }

        textBox.IsReadOnly = false;
        textBox.SelectAll();
    }

    private static void TextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        if (sender is not TextBox textBox)
        {
            return;
        }

        textBox.IsReadOnly = true;
    }

    #endregion
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top