如何在UserControl中使用ContentPresenter

Th1sD0t

我想创建一个UserControl(在本例中为带有定义Backgroundcolors的方形按钮),它可以承载其自己的内容。

UserControl:

<UserControl x:Class="SGDB.UI.Controls.ModernButton"
         xmlns:local="clr-namespace:SGDB.UI.Controls"
         xmlns:converter="clr-namespace:SGDB.UI.Converter"
         x:Name="_modernButton">
<Button>
    <Button.Resources>
        <converter:EnumToColorConverter x:Key="ColorConverter"/>
    </Button.Resources>
    <Button.Template>
        <ControlTemplate>
            <Border Width="{Binding Size, ElementName=_modernButton}" Height="{Binding Size, ElementName=_modernButton}" BorderBrush="Black" BorderThickness="0.8,0.8,3,3">
                <Grid Background="{Binding BackgroundColor, ElementName=_modernButton, Converter={StaticResource ColorConverter}}">
                    <ContentPresenter/>
                </Grid>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

现在,正如您所期望的,如果我在MainView中使用此Control,一切正常就可以了,直到定义了一些Content。

使用:

<control:ModernButton Size="200" BackgroundColor="Light">
    TEST
</control:ModernButton>

在这种情况下,“ TEST”将覆盖UserControl的全部内容(整个Button模板)。我猜发生这种情况是因为UserControl中的Button本身被定义为“ Content”,并且在定义新Content时将被覆盖。

所以最后一个问题是:是否有可能实现我想要的?如果是:如何?如何将我在MainView中定义的内容“重定向”到按钮模板内的自定义ContentPresenter而不是UserControls的ContentPresenter?

如果可能的话,我不想创建一个新的dp-propery来承载我的内容,例如:

<controls:MordernButton Size="200" BackgroundColor="Light">
    <controls:ModernButton.Content>
        I don't want this, if possible
    </controls:ModernButton.Content>
</controls:ModernButton>
Gopichandar

开始了。

<UserControl x:Class="SGDB.UI.Controls.ModernButton"
     xmlns:local="clr-namespace:SGDB.UI.Controls"
     xmlns:converter="clr-namespace:SGDB.UI.Converter"
     x:Name="_modernButton">

    <UserControl.Template>
        <ControlTemplate TargetType="UserControl">
            <Button Content="{TemplateBinding Content}">
                 <Button.Resources>
                    <converter:EnumToColorConverter x:Key="ColorConverter"/>
                  </Button.Resources>
            <Button.Template >
                <ControlTemplate TargetType="Button">
                    <Border Width="{Binding Size,
                                    ElementName=_modernButton}"
                    Height="{Binding Size,
                                     ElementName=_modernButton}"
                    BorderBrush="Black"
                    BorderThickness="0.8,0.8,3,3">
                        <Grid Background="{Binding BackgroundColor, ElementName=_modernButton, Converter={StaticResource ColorConverter}}">
                            <ContentPresenter />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Button.Template>
            </Button>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Windows 8.1中的ContentPresenter中使用模板选择器

来自分类Dev

如何在Windows 8.1中的ContentPresenter中使用模板选择器

来自分类Dev

如何在UserControl中使用抽象类

来自分类Dev

如何在UserControl中使用绑定的XAML属性?

来自分类Dev

如何在UserControl中使用绑定的XAML属性?

来自分类Dev

如何在ASP.NET中使用参数呈现UserControl

来自分类Dev

如何在C#Wpf UserControl中使用F#对象/记录

来自分类Dev

ContentPresenter如何在UWP中工作?

来自分类Dev

在jQuery的UserControl中使用RadioButtonList

来自分类Dev

代码隐藏中的WPF UserControl动画无法与ContentPresenter一起使用

来自分类Dev

代码隐藏中的WPF UserControl动画无法与ContentPresenter一起使用

来自分类Dev

如何在Scala中使用> =>?

来自分类Dev

如何在c ++中使用?

来自分类Dev

如何在SQL中使用NOT

来自分类Dev

如何在Haskell中使用(。)

来自分类Dev

如何在“ if”中使用空格?

来自分类Dev

如何在SQL中使用IF

来自分类Dev

如何在IndexError中使用If / Then

来自分类Dev

%如何在路径中使用

来自分类Dev

如何在Stata中使用*?

来自分类Dev

如何在TypeScript中使用“ then”?

来自分类Dev

如何在Haskell中使用(。)

来自分类Dev

如何在SQL中使用IF

来自分类Dev

如何在驴中使用&&?

来自分类Dev

如何在jQuery中使用or(||)

来自分类Dev

如何在<=中使用whereIn?

来自分类Dev

如何在类中使用 If

来自分类Dev

如何在grepl中使用“and”?

来自分类Dev

如何在宏中使用##?