我希望创建一个用户控件(在本例中为具有定义的背景颜色的正方形按钮),该控件可以托管自己的内容。
用户控件:
<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:ModernButton Size="200" BackgroundColor="Light">
TEST
</control:ModernButton>
在这种情况下,“TEST”将覆盖整个UserControl的内容(整个按钮模板)。我猜这是因为UserControl内部的按钮被定义为“Content”本身,当定义新内容时它将被覆盖。
所以最终的问题是:我正在寻找的是否可能实现?如果是:如何实现?我如何将我在MainView中定义的“Content”“重定向”到我按钮模板内部的自定义ContentPresenter,而不是UserControls的ContentPresenter?
如果可能,我不想创建一个新的dp属性来托管我的内容,例如:
<controls:MordernButton Size="200" BackgroundColor="Light">
<controls:ModernButton.Content>
I don't want this, if possible
</controls:ModernButton.Content>
</controls:ModernButton>