您好我是WPF新手。我使用
应用了圆角文本框<Window.Resources>
在一些网站上看到不知道是否最好。
现在我可能有4个文本框都有圆角 我选择了边框画笔颜色,并希望它在某些特定文本框具有焦点时应该更改。所以我添加了事件
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
textBoxCpanelUserName.BorderBrush = Brushes.OrangeRed;
}
但没有效果。我检查了当文本框具有焦点时事件触发但从不更改边框画笔的值。
以下是我的XMAL
<Window x:Class="AutomatingSomething.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800" Name="MainContainer" xmlns:my="clr-namespace:WPFControls.Clocks;assembly=WPFControls" xmlns:common="clr-namespace:WPF.Common">
<Window.Resources>
<ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}">
<Border Background="{TemplateBinding Background}"
x:Name="Bd" BorderBrush="#FF3BB5C8"
BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="7">
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Width" Value="Auto">
<Setter Property="MinWidth" Value="100"/>
</Trigger>
<Trigger Property="Height" Value="Auto">
<Setter Property="MinHeight" Value="20"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid Name="MasterGrid">
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF484848" Offset="0.075" />
<GradientStop Color="#FF8A8A8A" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<Border BorderBrush="Silver" BorderThickness="0" Height="45" HorizontalAlignment="Left" Margin="902,12,0,0" Name="border1" VerticalAlignment="Top" Width="88">
<my:RetroClock Name="retroClock1" FontSize="28" FontWeight="Normal" />
</Border>
<Button Content="Button" Name="button1" Margin="12,25,907,608" Click="button1_Click" />
<Grid common:VisibilityAnimation.AnimationType="Fade" Height="524" HorizontalAlignment="Left" Margin="101,25,0,0" Name="CpanelSettings" VerticalAlignment="Top" Width="665">
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFD4D4D4" Offset="0" />
<GradientStop Color="#FF797979" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<Label Content="Cpanel Settings" Height="38" HorizontalAlignment="Left" Margin="289,6,0,0" Name="label1" VerticalAlignment="Top" FontWeight="Bold" FontSize="26" FontFamily="Tekton Pro" FontStretch="Condensed" Width="147">
<Label.Foreground>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FFB4AFAF" Offset="1" />
</LinearGradientBrush>
</Label.Foreground>
</Label>
<TextBox Height="30" HorizontalAlignment="Left" Margin="132,50,0,0" Name="textBoxCpanelUserName" VerticalAlignment="Top" Width="187" FontFamily="Tekton Pro" FontWeight="Bold" BorderThickness="2" Template="{StaticResource TextBoxBaseControlTemplate}" FontSize="15" BorderBrush="#FF3BB5C8" GotFocus="textBox1_GotFocus" />
<Label Content="Cpanel Settings" FontFamily="Tekton Pro" FontSize="20" FontStretch="Condensed" FontWeight="Bold" Height="34" HorizontalAlignment="Left" Margin="6,46,0,0" Name="label2" VerticalAlignment="Top" Width="120">
<Label.Foreground>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FFB4AFAF" Offset="1" />
</LinearGradientBrush>
</Label.Foreground>
</Label>
</Grid>
</Grid>
</Window>
我还有一个问题,如果我绘制新的文本框并尝试通过绘制的模板使其角落圆,那么如果我更改焦点上的一个文本框上的颜色都会更新?(我不想要那样)
我非常抱歉,如果我说过任何非技术性的东西或说错了。那是因为我对wpf来说是全新的
答案 0 :(得分:3)
只需将以下Trigger添加到ControlTemplate.Triggers
即可<Trigger Property="IsFocused" Value="true">
<Setter Property="BorderBrush" TargetName="Bd" Value="Red"/>
</Trigger>
答案 1 :(得分:2)
我想你忘了在TextBoxBaseControlTemplate中绑定Bd的BorderBrush,就像你为Background做的那样。
模板应如下所示:
<ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}">
<Setter Property="BorderBrush" Value="#FF3BB5C8"/>
<Border Background="{TemplateBinding Background}"
x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="7">
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
...