user2408987
user2408987

Reputation: 101

UserControl examples

I am new to the WPF.

I wanted to create a UserControl which contains two fields for example

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="189*"/>
        <ColumnDefinition Width="328*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="53*"/>
        <RowDefinition Height="64*"/>
        <RowDefinition Height="62*"/>
        <RowDefinition Height="141*"/>
    </Grid.RowDefinitions>
    <TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="Name:" VerticalAlignment="Center"/>
    <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Center" Width="120" Grid.Column="1" Margin="3,0,0,0"/>

    <TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="Age:" VerticalAlignment="Center" Grid.Row="1"/>
    <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Center" Width="120" Grid.Row="1" Grid.Column="1" Margin="3,0,0,0"/>
    <Button Content="Add Person" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Grid.Row="2" Grid.Column="1"/>

</Grid>

I wanted to create my user control in such a way that other developers(Who uses my usercontrol) can be able to add extra fields to it.

Imagine a developer want to add "Address filed to the above control".

If any one have sample articles on this please provide me.

Upvotes: 0

Views: 506

Answers (1)

CharithJ
CharithJ

Reputation: 47530

I think you are after ContentControl. See below article for an example.

The ContentControl can contain any type of common language runtime object (such as a string or a DateTime object) or a UIElement object (such as a Rectangle or a Panel). This enables you to add rich content to controls such as Button and CheckBox.

How to Embed Arbitrary Content in a WPF Control

Upvotes: 1

Related Questions