简体   繁体   中英

How to set properties of textbox globally in 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.

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

!!! edit 2:

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?

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 ?

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>

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))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM