用最少的代码重现替换ControlTemplate(Replace ControlTemplate with minimum code reproduction)

编程入门 行业动态 更新时间:2024-10-26 04:30:31
用最少的代码重现替换ControlTemplate(Replace ControlTemplate with minimum code reproduction)

我想修改标准的WPF ComboBox以便在IsEditable设置为true时在左侧显示图像。

这就是我最终的结果:

具有以下内容的单独XAML文件中的ResourceDictionary :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"> <LinearGradientBrush x:Key="ButtonNormalBackground" StartPoint="0,0" EndPoint="0,1">...</LinearGradientBrush> <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/> <LinearGradientBrush x:Key="TextBoxBorder">...</LinearGradientBrush> <Style x:Key="ComboBoxFocusVisual">...</Style> <Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">...</Style> <Geometry x:Key="DownArrowGeometry">M 0 0 L 3.5 4 L 7 0 Z</Geometry> <Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">...</Style> <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">...</Style> <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">...</ControlTemplate> <Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">...</Style> </ResourceDictionary>

和Resources属性中的include指令,无论我想在哪里应用新模板:

<StackPanel.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="EditableComboBoxWithImage.xaml" /> </ResourceDictionary.MergedDictionaries> .... </ResourceDictionary> <StackPanel.Resources>

我只修改了<ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">...</ControlTemplate>一个小东西

我知道我不能只替换ControlTemplate的一部分,所以<ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">...</ControlTemplate>必须完全复制。

现在我的问题是:是否有可能通过链接到它们以某种方式摆脱ResourceDictionary的其他内容,因为它们是标准WPF的一部分?

I wanted to modify the standard WPF ComboBox to also show an image on the left when IsEditable is set to true.

This is what I ended up with:

A ResourceDictionary in a separate XAML file with these contents:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"> <LinearGradientBrush x:Key="ButtonNormalBackground" StartPoint="0,0" EndPoint="0,1">...</LinearGradientBrush> <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/> <LinearGradientBrush x:Key="TextBoxBorder">...</LinearGradientBrush> <Style x:Key="ComboBoxFocusVisual">...</Style> <Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">...</Style> <Geometry x:Key="DownArrowGeometry">M 0 0 L 3.5 4 L 7 0 Z</Geometry> <Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">...</Style> <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">...</Style> <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">...</ControlTemplate> <Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">...</Style> </ResourceDictionary>

and an include directive in the Resources property wherever I want to apply the new template:

<StackPanel.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="EditableComboBoxWithImage.xaml" /> </ResourceDictionary.MergedDictionaries> .... </ResourceDictionary> <StackPanel.Resources>

I only modified a tiny thing within <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">...</ControlTemplate>

I understand that I cannot replace only a part of a ControlTemplate, so <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">...</ControlTemplate> has to be fully reproduced.

Now my question is: is it possible to somehow get rid of the other contents of the ResourceDictionary by linking to them because they are part of standard WPF?

最满意答案

如果您询问是否可以从XAML中的默认ComboBox ControlTemplate引用原始内部控件,那么很遗憾答案是否,您不能 ......认为它们是类的private成员。

如果你不介意使用代码,那么你可以使用ControlTemplate类的FindName方法来提取它们,但实际上,它的工作量超过了它的价值:

TextBox editableTextBox = (TextBox)controlWithDefaultControlTemplateApplied.Template. FindName("PART_EditableTextBox", controlWithDefaultControlTemplateApplied);

If you are asking whether you can reference the original inner controls from the default ComboBox ControlTemplate in XAML, then unfortunately the answer is No, you can't... think about them being the private members of a class.

If you don't mind using code, then you can use the FindName method of the ControlTemplate class to extract them, but really, it's more work than it's worth:

TextBox editableTextBox = (TextBox)controlWithDefaultControlTemplateApplied.Template. FindName("PART_EditableTextBox", controlWithDefaultControlTemplateApplied);

更多推荐

Key,ResourceDictionary,电脑培训,计算机培训,IT培训"/> <meta name="descri

本文发布于:2023-07-19 02:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1171538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   ControlTemplate   Replace   reproduction   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!