Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dialog box. There is a text box inside the dialog box.When we open the dialogbox it should be focused and focus colour should be green, not blue(by default colour is blue)


i don't want <scrollviewer x:name="PART_ContentHost"> .but if I remove this my app name will disappear.

make this code cleaner with my requirements
requirements are:-
should highlight the textbox
highlight colour should be green
text content is App Name
isreadonly

What I have tried:

<textbox x:name="textBox" foreground="#00ff00" text="App Name" textwrapping="Wrap" grid.columnspan="2" isreadonly="True">
<textbox.style>

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="bg" BorderBrush="#55E41B" BorderThickness="2" Background="Black" FocusManager.FocusedElement="{Binding ElementName=textBox }">
<ScrollViewer x:Name="PART_ContentHost" />
</border>

</controltemplate>
</setter.Value>
</setter>
Posted
Updated 9-Dec-20 21:09pm

1 solution

In order to achieve this you should have an understanding how a WPF TextBox is actually constructed.
See here:wpf/TextBox.xaml at c271205b80c27df976acbd7236ec637090d127c1 · dotnet/wpf · GitHub[^]
According to this you can change the border colour by using a trigger which reacts on the IsFocused property.
XAML
<Style TargetType="TextBox">
	<Setter Property="Template">
		<Setter.Value>
			<ControlTemplate TargetType="{x:Type TextBox}">
				<Border x:Name="Border" 
						BorderBrush="{TemplateBinding BorderBrush}" 
						BorderThickness="{TemplateBinding BorderThickness}" 
						Background="{TemplateBinding Background}">
					<ScrollViewer x:Name="PART_ContentHost" 
								  Focusable="false" 
								  HorizontalScrollBarVisibility="Hidden" 
								  VerticalScrollBarVisibility="Hidden">
					</ScrollViewer>
				</Border>
				<ControlTemplate.Triggers>
					<Trigger Property="IsFocused" Value="true">
						<Setter Property="BorderBrush" 
								TargetName="Border" 
								Value="Green">
						</Setter>
					</Trigger>
				</ControlTemplate.Triggers>
			</ControlTemplate>
		</Setter.Value>
	</Setter>
</Style>

Now it's up to you to set the IsFocused property to true,
when starting the dialogue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900