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.