开发者

Change border Brush WPF

Hi there i am new to WPF.I have applied round corner text box by using

<Window.Resources> 

saw on some website do not know if it is best.

Now i may have 4 text boxes all having round corners I have selected a border brush color and want that it should change when ever the some particular textbox has focus.so i added event

private void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            textBoxCpanelUserName.BorderBrush = Brushes.OrangeRed;           
        }

but no effect.I checked when text box has focus the event fires but never changes the value of border brush.

Following is my 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.R开发者_C百科esources>
        <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>

I have one more question that if i draw new textbox and try to make its corner round by the drawn template then if i change the color on one textbox on focus all get updated?(i donot want that)


I am extremely sorry if i have said any non-technical thing or said the things wrong.Its because i am totaly new to wpf



Simply add following Trigger to ControlTemplate.Triggers

<Trigger Property="IsFocused" Value="true">
     <Setter Property="BorderBrush" TargetName="Bd" Value="Red"/>
</Trigger>


I suppose you forgot to bind Bd's BorderBrush in TextBoxBaseControlTemplate as you made it for Background.

Template should look like the following:

<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>
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜