I try to change color on my input box when is focused.
First I declare input button:
<TextBox x:Name="usernameTextBox"
HorizontalAlignment="Left"
Height="23"
Margin="115,31,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="277"
GotFocus="usernameTextBox_GotFocus"/>
And below I try to add style for that textbox
<Style x:Key="usernameTextBox"
TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused"
Value="true">
<Setter Property="Background"
Value="{StaticResource OnMouseOverColor}" />
</Trigger>
</Style.Triggers>
</Style>
Error:
Error 1 A value of type 'Style' cannot be added to a collection or dictionary of type 'UIElementCollection'. D:\\VS\\VIM\\VIM_WPF\\login.xaml 15 9 VIM_Wpf
Any other solution how to fix this?
Either you define your style as a resource (eg in your usercontrol / window's resources), and then do something like
<TextBox Style="{StaticResource theKeyOfYourStyle}" ..../>
or you explicitly set it in the TextBox:
<TextBox x:Name="usernameTextBox" HorizontalAlignment="Left" Height="23" Margin="115,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="277" GotFocus="usernameTextBox_GotFocus">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Background" Value="{StaticResource OnMouseOverColor}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.