2011-02-12 75 views
2

您好我是新來WPF.I已使用更改邊框刷WPF

<Window.Resources> 

應用圓角文本框中看到一些網站不知道這是否是最好的。

現在我可以有4個文本框都具有圓角 我選擇了一個邊框刷色,並希望它應該改變的時候有史以來一些特定的文本有focus.so我添加的事件

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

但沒有effect.I檢查時,文本框已焦點事件觸發,但從不改變邊界刷的價值。

以下是我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> 

我有一個問題,如果我吸引新的文本框,並嘗試通過繪製模板做出角落一輪那麼如果我改變顏色上的一個文本框上的焦點全部搞定更新?(我想DONOT那)


我感到非常抱歉,如果我說的任何非技術性的事情或說的話wrong.Its,因爲我完全以新的WPF


回答

3

只需添加以下觸發到ControlTemplate.Triggers

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

我想你忘了在TextBoxBaseControlTemplate中綁定Bd的BorderBrush,就像你爲Background創建BorderBrush一樣。

模板看起來應該像下面這樣:

<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

actully我複製弗羅姆別人的website.and是的,我認爲這是爲背景,但是當我在這裏更改邊框刷它會被更改 – 2011-02-12 18:01:37

+1

關於你的第二個問題。模板作爲每個元素的可視化樹的工廠。這意味着每個文本框都有它自己的模板表示形式,因此如果您更改一個文本框的顏色,這將不會對另一個文本框產生影響(因爲它們只共享工廠,而不是實際元素)。 – 2011-02-12 18:11:06