0
votes

I want to create a Custom Control that inherits WPF Grid with these features:

  • It must have some rows by default
  • It must have some child controls in those rows (mainly buttons and lines) by default
  • The non default content must be editable by designer or writting content in xaml of the window where the control is inserted

I tried inheriting Grid and adding content in the constructor but as soon as I add more content by designer, default content is lost. I have tried lots of things but I couldn't manage to make it. Is it even possible? How could I make this?

1

1 Answers

2
votes

Martin, You can't do that. Grid is a type of panel, with content as children property. So, if you add anything to it in XAML designer it will be relpaced.

However you can override childern property, and add this to your class <ContentProperty("PropertyName")> like in the example -

Ex:

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