簡體   English   中英

在Visual Studio中設置窗口樣式

[英]Style a window in visual studio

我的問題是,我創建了一個文本框,當有人單擊該文本框時,該文本框會加粗。 當我單擊屏幕上的其他位置時,我希望它變粗體。 現在,這是困難的部分,我需要在樣式表中完成此操作,並將.cs表連接到樣式表。 我的.cs工作表的內容是

using System.Windows;
using System.Windows.Input;
namespace SimTechGUI
{
    public partial class MyResourceDictionary : ResourceDictionary
{ 
   public MyResourceDictionary()
   {
      InitializeComponent();
   }
   private void Window_Focus(object sender, MouseButtonEventArgs e)
   {
       Keyboard.ClearFocus();
   }
}
}

我的XAML樣式表看起來像

<ResourceDictionary      xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"  
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                x:Class="SimTechGUI.MyResourceDictionary"
                x:ClassModifier="public">

<Style TargetType="{x:Type Window}">
    <EventSetter Event="MouseDown" Handler="Window_Focus" />
</Style>


<Style TargetType="{x:Type TextBox}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="MinWidth" Value="120" />
    <Setter Property="MinHeight" Value="25" />
    <Setter Property="AllowDrop" Value="true" />
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border Name="Border" CornerRadius="6" Padding="2" BorderBrush="Black" BorderThickness="2,1">
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="BorderThickness" TargetName="Border" Value="3"/>                            
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

我所擁有的目前無法正常工作。 有誰知道我要做這個工作需要做什么。

如果您直接在Window上設置MouseDown事件處理程序,則您的代碼可以正常工作

MouseDown="Window_Focus"

應用樣式的方式,Window_Focus Handler永不命中。 在Keyboard.ClearFocus()上放置一個斷點; 看看是否成功?

您可以按名稱應用樣式明確定義如下樣式:

        <Style x:Key="windowStyle" TargetType="Window">
            <EventSetter Event="MouseDown" Handler="Window_Focus" />
        </Style>

並在所有Windows上執行以下操作:

Style="{StaticResource windowStyle}"

我認為不允許您隱式指定樣式的原因可能是已經將樣式應用於所有Windows:

看一下這些帖子: 哪些原因可能阻止應用顯式和隱式樣式?

全局樣式在WPF中不起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM