2015-12-02 78 views
2

我一直在嘗試在getFocus事件的WPF應用程序中更改我的文本框的背景。在winforms中,我做了這樣的事情,它運行良好。如何更改getFocus上的文本框背景顏色

class Format_GUI 
     { 
      public void center_groupbox(Control cntrl, Form ms_form) 
       { 
       cntrl.Left = (ms_form.ClientSize.Width - cntrl.Width)/2; 

       } 


      public void color_control(Control myControl) 
       { 
       Control inst_Control = new Control(); 
       inst_Control = myControl; 
       inst_Control.BackColor = System.Drawing.ColorTranslator.FromHtml("#E55451"); 
       } 

}

然後我可以呼籲這樣的輸入事件的類功能:

private void txtTextBox1_Enter(object sender, EventArgs e) 
    { 
     myGUI.highlight_SelectedControl(txtTextBox1); 
    } 

這WinForms的正常工作。有沒有人有一個想法如何在WPF應用程序中做到這一點?

回答

0

只需轉到XAML中的TextBox元素並添加GotFocus事件。 這將在後面的代碼中分配一個新的處理程序。 您必須先發送發件人,然後才能訪問TextBox。

private void txtTextBox1_GotFocus(object sender, RoutedEventArgs e) 
{ 
    TextBox tbox = sender as TextBox; 
    tbox.Background = Brushes.Red; 
} 

如果您想將其更改回來,還有一個LostFocus事件。

1

首先,TextBoxGotFocus事件。使用x:Name指令,使箱從代碼訪問...

在XAML ...

<TextBox x:Name="TextBox" GotFocus="Handler"/> 

,然後在後臺代碼...

private void Handler(object sender, RoutedEventArgs args) 
{ 
    TextBox.Background = new SolidColorBrush(Color.FromArgb(...)); 
} 

然而,在WPF ,也有使用Binding

<TextBox> 
    <TextBox.Style> 
     <Style TargetType="Control"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsFocused, Mode=OneWay, RelativeSource={RelativeSource Self}}" 
          Value="True"> 
        <Setter Property="Background" Value="#E55451"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </TextBox.Style> 
</TextBox> 

的選項,這將導致不僅着色行爲,但也可以在未聚焦時將其還原,並且不需要依賴事件,因此相同的代碼可以應用於任何種類的控制。與任何樣式一樣,這也可以放置在資源字典中被使用,可能附着到類型本身。

<!-- Resources under App.xaml. This can also be embedded in a ResourceDictionary --> 
<Resources> 
    <Style x:Key="{x:Type TextBox}" TargetType="Control"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding IsFocused, Mode=OneWay, RelativeSource="{RelativeSource Self}}" 
         Value="True"> 
       <Setter Property="Background" Value="#E55451"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
<Resources> 
+0

非常感謝。這對我很好。但是,如果我的表單上有很多文本框,是否有辦法讓所有這些文本框都完成,而無需爲每個表單編寫代碼? – ncastillo

+0

是的,這就是風格之美!我將把它整合到我的答案中。 請注意,@ pikciu也有一個很好的答案,它將DataTriggers簡化爲Triggers,我並沒有意識到這一點。我建議你使用他自己的代碼。 – David

0

所有你需要的是這個在您的關注事件,其更改爲散列碼

((TextBox)sender).Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#E55451")); 
0

查找元素在XAML和添加事件,即:

<TextBox Height="23" Name="textBox1" Width="120" GotFocus="textBox1_GotFocus" LostFocus="textBox1_LostFocus" /> 

並設置顏色:

private void textBox1_GotFocus(object sender, RoutedEventArgs e) 
{ 
    textBox1.Background = Brushes.Yellow; 
} 

private void textBox1_LostFocus(object sender, RoutedEventArgs e) 
{ 
    textBox1.Background = Brushes.White; 
} 
1

Imho最好的方法是在XAML中使用觸發器

<TextBox> 
     <TextBox.Style> 
      <Style TargetType="TextBox"> 
       <Style.Triggers> 
        <Trigger Property="TextBox.IsFocused" 
          Value="True"> 
         <Setter Property="TextBox.Background" 
           Value="#E55451" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </TextBox.Style> 
    </TextBox>