如何在App.xaml中[C#]全局设置文本框的属性 [英] How to set properties of textbox globally in app.xaml [C#]

查看:197
本文介绍了如何在App.xaml中[C#]全局设置文本框的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序,我有所有的逻辑,但有一个从前端而我不是那么熟悉的角度做一些工作。

I am writing an application and I have all logic, but there is some work from perspective of frontend which I am not so familiar.

我学会如何我可以设定所有按钮的设计全球。同样的方式,我试图做的文本框。事实上,样式应用,但应用这种风格后,我不能插入任何文本放入文本框...
我该如何解决?问题是什么 ?可能有人建议我有什么不好?

I have learn how I can set globally design of all buttons. Same way I was trying to do textbox. Indeed the style is applied but I can not insert any text into textbox after this style is applied... How I can fix it ? What is the problem ? May anybody suggest me what is wrong ?

我想要做的只是使周围所有的文本框和按钮,红色细边框,而有人用鼠标在他们去。 ,而且有一个下拉框,我想以同样的风格,作为以及

What I wanted to do is just make red thin border around all textboxes and buttons while someone is going with mouse over them. And there is one combobox which I would like to serve in the same style as well.

这是我的App.xaml代码:

This is my code in 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>



编辑:按钮的方式

edit: buttons are working properly by the way

!!!编辑2:

我试图解决我的方式组合框,但是任何与控件模板的ScrollViewer都不组合已经给成功。这是缺乏在我的申请的一个组成部分。我已阅读并尝试像这样的,例如许多解决方案:如何风格鼠标悬停组合框的背景?

I was trying to fix combobox on my way, however any combination with ControlTemplate neither ScrollViewer have give the success. This is the only one component which is lacking in my application. I have read and try many solutions like this one for example: How to style ComboBox Background on Mouse Hover?

由于,我想请的解决方案。因为我专注于编程逻辑部分XAML的更复杂的部分的整体认识是不相关的,我只是一个高亮组合框。
我想有这改变颜色边框,而鼠标在红色和组合框的其余部分应保持标准的组合框。仅这一个属性。我怎么可能会迅速改变呢?

Because of that I would like to please for solution. The whole understanding of more complex part of xaml is not relevant for me for just one highlighting of combobox since I am focused on logical part of programming. I would like to have a combobox which is changing the color border while the mouse is over for red and the rest of combobox should remain standard. Just this one property. How I may change it quickly ?

推荐答案

文本框期望名称为PART_ContentHost控制它可以把它的内容。实际上,在文本框的ScrollViewer的默认模板用于这一点,所以解决的一种方式是,以取代ContentPresenter:

TextBox expects control with name "PART_ContentHost" where it can place it's content. Actually in default template of TextBox ScrollViewer is used for this, so one way to fix is to replace your ContentPresenter with:

<ScrollViewer x:Name="PART_ContentHost" />



另外,您可以使用您的边境的内容主机:

Alternatively you can use your Border as content host:

<ControlTemplate TargetType="TextBox">
     <Border CornerRadius="2"
             BorderThickness="2"
             BorderBrush="{TemplateBinding BorderBrush}"
             Background="{TemplateBinding Background}"
             x:Name="PART_ContentHost" />
          <ControlTemplate.Triggers>
              <Trigger Property="IsMouseOver" Value="True">
                  <Setter TargetName="PART_ContentHost"
                          Property="BorderBrush"
                          Value="#FF0000" />
              </Trigger>
          </ControlTemplate.Triggers>
</ControlTemplate>



更新:为什么PART_ContentHost?假设你有一些控制的作者。您尝试设计自己的控制,以便它可以与任何的ControlTemplate其他人将提供工作。但是,这并不总是可能的,有时你需要模板的特定部分,以匹配特定的条件。在这种情况下,文本框的作者发现的控制在它的模板,可以是的ScrollViewer或装饰,并有一个名为PART_ContentHost。他后来与控制确实是无关紧要我们:文本框只需要一个名为PART_ContentHost控制是在模板正常运作。如何控制的作者可以表达这样的要求?通过 TemplatePart 属性。此属性不做任何事,除了表达像上面的要求。如果你看一下TextBoxBase控制,从TextBox继承,您将看到:

UPDATE: why PART_ContentHost? Suppose you are the author of some control. You try to design your control so that it can work with any ControlTemplate other people would provide. But that is not always possible, sometimes you need specific parts of your template to match certain criteria. In this case, author of TextBox finds a control in it's template, which is either ScrollViewer or Decorator, and has a name "PART_ContentHost". What he later does with that control is irrelevant for us: TextBox just requires control with name "PART_ContentHost" to be in template for normal functioning. How author of control can express such requirements? Via TemplatePart attribute. This attribute does nothing except expressing the requirements like above. If you look at TextBoxBase control, from which TextBox inherits, you will see:

[TemplatePart(Name = "PART_ContentHost", Type = typeof (FrameworkElement))]

这篇关于如何在App.xaml中[C#]全局设置文本框的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆