简体   繁体   中英

Style for template part in the WPF Custom Control

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

Your example code is trying to set the Template property of the CustomControl itself and not that of the TextBox contained within the CustomControl . It's hard to answer a question like this when you haven't shown the XAML of the TextBox that you say you want to change the ControlTemplate of. As it is, I can only 'guide' you on how this might be done on any TextBox :

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

In the future, please provide all of the relevant code in order to help you get answers quickly and accurately.


UPDATE>>>

Ok, after your question edit, I think I understand your question now. However, you cannot do this without either changing the code of your CustomControl or providing a new ControlTemplate for it (something like my first example).

If you could change the code, you would first need to create your NewTextValue DependencyProperty as the correct type that you want to use, eg. a Brush . Then you'd have to alter the XAML definition of the CustomControl to include this:

<TextBox Background="{Binding NewTextValue}">

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