WPF仅在combBox的PART_EditableTextBox上更改属性 [英] WPF altering property on just PART_EditableTextBox of a combBox

查看:243
本文介绍了WPF仅在combBox的PART_EditableTextBox上更改属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对默认wpf组合框的PART_EditableTextBox进行更改(例如,更改背景).

我尝试添加如下样式:

<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate TargetType="ComboBox">
        <TextBox x:Name="PART_EditableTextBox" Background="AntiqueWhite"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

但这会导致默认模板的其余部分被忽略.

有没有办法可以覆盖PART_EditableTextBox的特定属性,还是必须复制整个控件模板并在其中进行更改?

解决方案

一些更改控件外观的方法...

复制控件模板,对其进行编辑并以样式使用它

在复制和修改控件的模板时...您必须牢记1件事...主题.

根据主题(即不同的镶边),控件可能具有完全不同的模板设计...因此您的问题是...您选择复制,修改然后在控件上使用哪个模板.

无论选择哪个,都有问题...当某人正在以与从其复制模板的主题不同的主题运行Windows时,该控件将看起来是错误的/不正确.

要查看不同模板在不同主题中的外观...使用ShowMeTheTemplate:

因此,要正确执行此操作,您必须复制和修改每个主题(经典,Luna,Aero,Royale等)的模板,并执行必要的步骤来获取不同的主题模板,以便在主题更改时加载...这样您的控件就可以主题识别"了.


在运行时应用模板后修改可视树

如果您是控件的作者或创建控件的派生版本...则可以等到应用模板后,再在OnApplyTemplate中...然后可以寻找零件"在视觉树中,然后在运行时修改视觉树/更改属性(即,您可以更改PART_EditableTextBox的背景).

但是,如果您依赖隐式样式(如您所愿),或者不希望或无法用XAML中的派生版本替换所有控件,则此方法将无效.


在运行时获取ControlTemplate的副本,然后修改零件"

还有另一种可能....在运行时获取控件的ControlTemplate ...(将用于当时设置的当前主题)....对其进行修改,然后将其设置到Control上./p>

这样做的好处是,如果系统中存在一些新主题,而您没有对其主题名称的事先了解(因此也没有为其设计模板),那么可以代替您的控制模板来自通用主题(因此不合时宜)....您有更好的机会使外观更好地适应新主题.但这有点骇人听闻.

I would like to make a change to the PART_EditableTextBox of the default wpf combobox (change the background for example).

I tried adding a style like this:

<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate TargetType="ComboBox">
        <TextBox x:Name="PART_EditableTextBox" Background="AntiqueWhite"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

But that causes the rest of the default template to be ignored.

Is there a way I can just override a specific property of the PART_EditableTextBox or do I have to copy over the entire control template and make my change in there?

解决方案

Some different ways to change the look of the Control...

Copying a Controls Template, Editing it and Using it in a Style

When copying and modify the Template of a control...you have to bear in mind 1 thing...themes.

The control might have completely different Template designs depending on the theme (i.e. different chrome)...and so your problem is...which template do you choose to copy, modify and then use on your control.

No matter, which one you choose...you have a problem...when someone is running Windows in a theme that is different to the theme from which you copied the Template from...well that control will look wrong/out of place.

To see how different Templates can look like in different themes...use ShowMeTheTemplate:

So to do it properly, you would have to copy and modify the template for each theme (Classic, Luna, Aero, Royale, etc) and do the necessary steps to get your different themed template to get loaded when the theme changes...so that your control is "theme aware".


Modifying the Visual tree After Template has been applied at runtime

If you were the author of the control or you create a derived version of a control...then you can wait till the template is applied and then in OnApplyTemplate...you can then hunt for the "Part" in the visual tree, and then modify the visual tree/changes attributes at runtime (i.e. you could change the Background of PART_EditableTextBox).

However, this doesn't work if you are relying on implicit styles (as you are), or don't want to or can't replace all your controls with the derived version in your XAML.


Obtaining a Copy of the ControlTemplate at runtime, and modifying the "Part"

There is another possibility....obtaining the ControlTemplate for a control at runtime...(which will be for the current theme set at the time)....modifying it, then setting it onto the Control.

The beauty of this is if there are new themes in the system for which you didn't have prior knowledge of their names (and thus didn't design a Template for it), then instead of your control Template being picked up from Generic theme (and thus being out of place)....you have a better chance for the look to fit in better with the new theme. But it is a bit of a hack.

这篇关于WPF仅在combBox的PART_EditableTextBox上更改属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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