我想创建一个自定义控件,它使用以下功能继承 WPF Grid :
我尝试继承Grid并在构造函数中添加内容,但是一旦我按设计器添加更多内容,默认内容就会丢失。我尝试了很多东西,但我无法成功。它甚至可能吗?我怎么能这样做?
答案 0 :(得分:2)
但是你可以覆盖childern属性,并将其添加到类<ContentProperty("PropertyName")>
中,如示例所示 -
例如:
'Code:
<ContentProperty("Children")> _
Public Class MyGrid
Public Overloads ReadOnly Property Children As UIElementCollection
Get
Return Me.ContentGrid.Children
End Get
End Property
End Class
'Markup
<Grid x:Class="MyGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" />
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Grid.Row="2" />
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Grid.Row="1" />
<Grid Name="ContentGrid" Grid.RowSpan="3"></Grid>
</Grid>