0

我们如何为作为 WPF 中自定义控件一部分的控件设置样式?

4

1 回答 1

0

您的示例代码试图设置自身的Template属性,而不是. 当您没有显示您说要更改的XAML 时,很难回答这样的问题。事实上,我只能“指导”你如何在任何:CustomControlTextBoxCustomControlTextBoxControlTemplateTextBox

<Style TargetType="MyCustomControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CustomControl}">
                <Grid>
                    ...
                    <TextBox Background="{Binding SomeBrushProperty}">
                        <TextBox.Template>
                            <ControlTemplate TargetType="{x:Type TextBox}">
                                <!--Define your TextBox ControlTemplate here-->
                            </ControlTemplate>
                        </TextBox.Template>
                    </TextBox>
                    ...
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

今后,请提供所有相关代码,以帮助您快速准确地得到答案。


更新>>>

好的,在你的问题编辑之后,我想我现在明白你的问题了。但是,如果不更改您的代码CustomControl或为其提供新代码ControlTemplate(类似于我的第一个示例),您就无法做到这一点。

如果您可以更改代码,则首先需要将您的代码创建NewTextValue DependencyProperty为您要使用的正确类型,例如。一个Brush。然后,您必须更改 的 XAML 定义CustomControl以包含以下内容:

<TextBox Background="{Binding NewTextValue}">
于 2013-10-24T12:33:58.343 回答