0

I am trying to get a reference to a TextBox defined in a RecourceDictionary. Usually, setting a Name property is enought to get a reference in the code, but not when an element is defined in ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:my_namespace"
                    x:Class="my_namespace.ListBoxItemControlTemplate"
                    x:ClassModifier="public">

                        <ControlTemplate x:Key="CustomLBoxItem" TargetType="LBoxItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

                        <TextBox x:Name="CustomTextBox" <!-- this will not work -->  Height="24" BorderThickness="0"  MinWidth="310" MaxWidth="310" Cursor="Hand" VerticalAlignment="Center" Text="{Binding MatchText}">
                            <TextBox.Style>
                                ...                
                                </Style>
                            </TextBox.Style>
                        </TextBox>

    </ControlTemplate>
  </ResourceDictionary>

In this case, CustomTextBox will not work.

 namespace my_namespace{
 public partial class ListBoxItemControlTemplate : ResourceDictionary {
  

    private void MainWindow_MouseDownEvent() {
        Console.WriteLine("ouuuuut");
        this.CustomTextBox.CaretBrush = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); // will not work.

    }
 }
}

What I am trying to achieve is to hide the cursor when mouse down is fired outside the TextBox. For that I defined an event in MainWindow:

public partial class MainWindow : Window, INotifyPropertyChanged {

   public delegate void MouseDownDelegate();
   public static event MouseDownDelegate MouseDownEvent;

    
   private void DragWindow_MouseDown(object sender, MouseButtonEventArgs e) {
       MouseDownEvent();
   }

}

How exactly can I get a reference to the TextBox element? I need it so I can hide the caret.

Robert Segdewick
  • 543
  • 5
  • 17
  • "*a reference to **the** TextBox element*" - which one, when the ControlTemplate is applied multiple times? You probably just want to hide the caret when the TextBox has no focus - which might be done by a Style Trigger. – Clemens Jul 27 '20 at 14:12
  • It is indeed applied multiple times. Perhaps getting a reference to the list of all TextBox elements with a specified name would be a better question. – Robert Segdewick Jul 27 '20 at 14:15
  • Not sure what you actual problem is. As soon as a TextBox loses focus, the caret is hidden anyway. – Clemens Jul 27 '20 at 14:19
  • It does not lose focus when I click away. For that, I tried to manually hide caret. "LostFocus" is never triggered for example – Robert Segdewick Jul 27 '20 at 14:21
  • Sorry, I'm not aware of an elegant solution, but you may move focus to the TextBox's parent element like e.g. this: https://stackoverflow.com/a/6489274/1136211. There would still be an event handler in code behind, but one that's used for all items. – Clemens Jul 27 '20 at 14:28
  • Even using grid.Focus() (I do have a an element named grid) or this.Focus in the MainWindow mouse down event handler will not cause focus loss on TextBox. I am all open for ugly solutions as well – Robert Segdewick Jul 27 '20 at 14:36
  • You would simply call `((FrameworkElement)sender).Focus();` – Clemens Jul 27 '20 at 14:38
  • it is bizarre, but calling Focus() on window mousedown does not cause TextBox to lose focus. The only way I found I can hide the caret was by simply changing it's color. – Robert Segdewick Jul 27 '20 at 14:43
  • There should be a focusable container element in the ControlTemplate which has the event handler. – Clemens Jul 27 '20 at 15:12

1 Answers1

0

This is what worked:

        private TextBox textBoxOuter;
        private void MainWindow_MouseDownEvent() {
            this.textBoxOuter.CaretBrush = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
            MainWindow.MouseDownEvent -= MainWindow_MouseDownEvent;
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e) {
            textBoxOuter = textBox;
            this.textBoxOuter.CaretBrush = null;
            MainWindow.MouseDownEvent += MainWindow_MouseDownEvent;
        }
Robert Segdewick
  • 543
  • 5
  • 17