【问题标题】:Style a window in visual studio在 Visual Studio 中设置窗口样式
【发布时间】:2015-06-11 16:27:33
【问题描述】:

我的问题是我创建了一个文本框,当有人点击它时会加粗。当我单击屏幕上的其他位置时,我希望它不加粗。现在这是我需要在我的样式表中执行此操作的困难部分,并且 .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>

我所拥有的目前不起作用。有谁知道我必须做什么才能完成这项工作。

【问题讨论】:

    标签: c# wpf visual-studio xaml styles


    【解决方案1】:

    如果您直接在 Window 上设置 MouseDown 事件处理程序,您的代码可以正常工作

    MouseDown="Window_Focus"
    

    按照您应用样式的方式,Window_Focus 处理程序永远不会命中。在 Keyboard.ClearFocus() 上放置断点;看看有没有命中?

    【讨论】:

    • 它没有命中。另外,我需要在样式页面上这样做,这样我就不必在每次将新窗口添加到项目时都添加它。
    【解决方案2】:

    您可以通过名称显式应用样式 定义这样的样式:

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

    并像这样应用到所有 Windows:

    Style="{StaticResource windowStyle}"
    

    我认为它不允许您隐式指定样式的原因可能是已经将样式应用于所有 Windows:

    看看这些帖子: What reasons could prevent explicit and implicit styles from applying?

    Global Style not working in WPF

    【讨论】:

    • 我想不用碰窗户也能做到。只有我的样式表和连接到它的 .cs。在启动线程之前,我已经在窗口中工作了我试图在样式表中找到一种方法。
    猜你喜欢
    • 2010-09-26
    • 2011-04-15
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多