[英]Overriding IsMouseOver on ControlTemplate makes Button disappear
[英]ControlTemplate makes TextBox content disappear
我想對我的TextBox
應用一種樣式,所以我在ResourceDictionary
中添加了一種樣式。 但是當涉及到ControlTemplate
時,它會讓我的 TextBoxes 崩潰。 當我刪除<ControlTemplate>
塊時,TextBoxes 再次工作。
有誰知道如何將我的 ControlTemplate 應用於我的文本框樣式?
我對xaml
元素的資源字典完全陌生,所以我不知道如何解決這個問題......
PS:在同一個文件中,我的按鈕有另一種樣式,可以嗎? 還是我需要為每種樣式創建單獨的文件?
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontFamily" Value="Gotham Light"/>
<Setter Property="FontWeight" Value="Light"/>
<Setter Property="Background" Value="AliceBlue"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="Cycle" />
<Setter Property="Template">
<Setter.Value>
<!-- #region this is where the code doesn't work -->
<ControlTemplate TargetType="{x:Type TextBox}">
<Border Background="{TemplateBinding Background}" CornerRadius="5"
BorderThickness="1"
BorderBrush="White">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
<!-- #endregion -->
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="White"/>
<Setter Property="Opacity" Value="0.9"/>
<Setter Property="Foreground" Value="#96c80a"/>
</Trigger>
</Style.Triggers>
</Style>
它不起作用,因為您告訴 ControlTemplate 顯示一個空白TextBlock
。 在線查看 TextBox 的默認 ControlTemplate ,您會看到為了顯示文本框,您需要在 ControlTemplate 中使用硬編碼名稱“PART_ContentHost” label 一個 ScrollViewer。
將您的 ControlTemplate 更改為這樣,它將再次起作用:
<ControlTemplate TargetType="{x:Type TextBox}">
<Border Background="{TemplateBinding Background}" CornerRadius="5"
BorderThickness="5"
BorderBrush="Black">
<ScrollViewer Margin="0" x:Name="PART_ContentHost" />
</Border>
</ControlTemplate>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.