简体   繁体   English

如何将自定义TextBox样式应用于内置控件?

[英]How can I apply a custom TextBox style to built-in controls?

Background: 背景:

In a standard WPF application, right clicking on a TextBox displays a ContextMenu with three commands: Cut, Copy, and Paste. 在标准WPF应用程序中,右键单击TextBox会显示带有三个命令的ContextMenu:剪切,复制和粘贴。 My desire is to add a Select All command to all TextBox ContextMenus in my application. 我的愿望是向应用程序中的所有TextBox ContextMenus添加“全选”命令。

Problem: 问题:

My standard approach is to create a Style that targets TextBox and supplies the new ContextMenu. 我的标准方法是创建一个针对TextBox的样式,并提供新的ContextMenu。 That way, all TextBox controls inherit the style and pick up the change. 这样,所有TextBox控件都会继承样式并进行更改。

The trouble is that my style isn't inherited by controls that contain TextBoxes. 问题在于我的样式没有被包含TextBoxes的控件继承。 For instance, when editing a DataGridTextColumn cell, I know a TextBox is used but it doesn't inherit my style. 例如,当编辑DataGridTextColumn单元格时,我知道使用了TextBox,但它不继承我的样式。 The same goes for some 3rd party controls used by my application. 我的应用程序使用的某些第三方控件也是如此。

Question: 题:

Is there some other way to have controls, like the DataGridTextColumn's cell, pick up my style changes, or am I stuck altering their templates? 还有其他方法可以使用控件,例如DataGridTextColumn的单元格,进行样式更改,还是我不得不更改其模板?

Addendum: 附录:

This is the style: 这是样式:

<Style 
    TargetType="{x:Type TextBox}"
    BasedOn="{StaticResource {x:Type TextBox}}"
    >
    <Setter
        Property="ContextMenu"
        >
        <Setter.Value>
            <ContextMenu>
                <MenuItem
                    Header="Cu_t"
                    Command="ApplicationCommands.Cut"
                    InputGestureText="Ctrl+X"
                    />
                <MenuItem
                    Header="_Copy"
                    Command="ApplicationCommands.Copy"
                    InputGestureText="Ctrl+C"
                    />
                <MenuItem
                    Header="_Paste"
                    Command="ApplicationCommands.Paste"
                    InputGestureText="Ctrl+V"
                    />
                <Separator
                    />
                <MenuItem
                    Header="Select _All"
                    Command="ApplicationCommands.SelectAll"
                    InputGestureText="Ctrl+A"
                    />
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

your textbox style will be reflected to textboxes who dont have any style applied. 您的文本框样式将反映给未应用任何样式的文本框。 So if you have any third party textbox and it has already a style applied with key. 因此,如果您有任何第三方文本框,并且该文本框已经具有使用键的样式。 your style will fail. 你的风格会失败。 you may need to use expression blend to open the the control template and change style or understand how the textbox style applied there. 您可能需要使用表达式混合来打开控件模板并更改样式,或者了解如何在其中应用文本框样式。

cheers.. 干杯..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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