Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function call within XAML code?

I'd like to set a style on all my TextBox controls that does the following when it receives keyboard focus:

1) Change the background color
2) Call .SelectAll() to highlight all text

I have this so far:

<Style TargetType="TextBox">
<Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Color="#FFFFD1D9"/>
                    </Setter.Value>
                </Setter>
           </Trigger>
</Style.Triggers>
</Style>

Is there a way to also call .SelectAll() ? Thanks.

like image 209
Matt H. Avatar asked Jun 14 '10 23:06

Matt H.


People also ask

How do you call a method in XAML?

Easiest way is to install a nuget. In the example below the namespaces are defined as xmlns:i and xmlns:ei . Show activity on this post. You Can create RelayCommand inheriting ICommand, and then create property of ICommand and assign relay command to that property and call the method.

Is XAML front end?

WPF uses XAML as its frontend language and C# as its backend languages. WPF was introduced as a part of . NET Framework 3.0 as the Windows library to build Windows client apps and the next generation of Windows Forms.

How do you draw a curve in WPF?

To create a cubic Bezier curve, use the PathGeometry, PathFigure, and BezierSegment classes. To display the resulting geometry, use a Path element, or use it with a GeometryDrawing or a DrawingContext. In the following examples, a cubic Bezier curve is drawn from (10, 100) to (300, 100).


1 Answers

You can do this using attached behaviours.

Example

public static class TextBoxBehaviour
{
    public static bool GetSelectAll(TextBoxBase target)
    {
        return (bool)target.GetValue(SelectAllAttachedProperty);
    }

    public static void SetSelectAll(TextBoxBase target, bool value)
    {
        target.SetValue(SelectAllAttachedProperty, value);
    }

    public static readonly DependencyProperty SelectAllAttachedProperty = DependencyProperty.RegisterAttached("SelectAll", typeof(bool), typeof(TextBoxBehaviour), new UIPropertyMetadata(false, OnSelectAllAttachedPropertyChanged));

    static void OnSelectAllAttachedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        ((TextBoxBase)o).SelectAll();
    }
}

Usage

<Style TargetType="{x:Type TextBox}" xmlns:behaviours="clr-namespace:Controls.Behaviours">
  <Style.Triggers>
      <Trigger Property="IsKeyboardFocusWithin" Value="True">
          <Setter Property="Background" Value="#FFFFD1D9"/>
          <Setter Property="behaviours:TextBoxBehaviour.SelectAll" Value="True"/>
     </Trigger>
  </Style.Triggers>
</Style>

References

  • Josh Smith on Attached Behaviours

NB: Wasn't able to test the above implementation, in theory though it should just work™.

HTH,

like image 180
Dennis Avatar answered Sep 28 '22 01:09

Dennis