我正在編寫一個應用程序,我擁有所有的邏輯,但是從前端角度來看,我還不是很熟悉。如何在app.xaml中全局設置文本框的屬性[C#]
我已經學習如何設置所有按鈕的全局設計。我試圖做文本框的方式也一樣。事實上,風格是應用的,但我不能插入任何文本到這個風格應用後的文本框... 我該如何解決它?問題是什麼 ?有人會建議我什麼是錯的?
我想要做的只是在所有文本框和按鈕周圍製作紅色細邊框,而有人則將鼠標放在它們上方。還有一個組合框,我想以相同的風格進行操作。
這是我的App.xaml代碼:
<Application x:Class="Application.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Application"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
CornerRadius="2"
BorderThickness="2"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#FF0000" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TextBox">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<!--<Setter Property="Foreground" Value="#000000"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border Name="textbox"
CornerRadius="2"
BorderThickness="2"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="textbox" Property="BorderBrush" Value="#FF0000" />
<!--<Setter Property="Foreground" Value="#FF0000"/>-->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
編輯:按鈕的方式
正常工作!編輯2:
我試圖在我的方式修復組合框,但是與ControlTemplate的任何組合都不ScrollViewer已成功。這是我的應用程序中唯一缺乏的組件。我已閱讀並嘗試了許多解決方案,例如這樣一個例如:How to style ComboBox Background on Mouse Hover?
因此,我想請求解決方案。由於我專注於編程的邏輯部分,所以對於更復雜的xaml部分的全面理解對於我來說並不重要,因爲它只是突出顯示了組合框。 我想要一個組合框,它改變了顏色邊界,而鼠標是紅色的,其餘的組合框應保持標準。只有這一個屬性。我如何快速改變它?
您能否提供使用此樣式的文本框示例? – osmanraifgunes
1月 - 您可能想將您的「編輯2」作爲單獨的帖子,因爲您的原始問題(文本框)已被標記爲已回答,並且組合框問題是不同的。 –