Display Custom WPF User Control using Code

Posted on 2009-02-13
Last Modified: 2013-11-12
Hi to all,

I am working on a WPF application that load Items from my database into a WPF Listview in a Gridview View. I have a Checkbox next to each loaded item. When the checkbox is checked I want to display a custom user control, but for the life of me can't figure out how to do that. The fact that I have been working 2 days straight on this might have to do with it and I would really appreciate any help with this. I can't load the user control for each Item (I think), because there is a minimum of 500 Items. This must be done by code.

Thanks for the help
Question by:ALawrence007
    LVL 24

    Accepted Solution

    1. You'll need to handle a OnChange event for the check boxes.
    2. Create a new instance of the WPF user control
    3. Add it to an appropriate container.

    Unfortunately, xaml can't be uploaded to the site, so here's my sample project:

    Class Window1 
        Private Sub CheckBox1_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles CheckBox1.Checked
            Dim uc As New TestControl
        End Sub
    End Class
    -- Window1.xaml
    <Window x:Class="Window1"
        Title="Window1" Height="300" Width="300">
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            <CheckBox Margin="0,0,0,0" Name="CheckBox1" Grid.Row="0">Check me to add a user control</CheckBox>
            <StackPanel Name="StackPanel1" Grid.Row="1"></StackPanel>
    -- TestControl.xaml
    <UserControl x:Class="TestControl"
        xmlns:x="" Width="200" Height="200">
        <Rectangle Height="200" Name="Rectangle1" Stroke="Black" Fill="LimeGreen" Width="200" />

    Open in new window


    Author Closing Comment

    Thanks Chaosian, this helped me find my answer.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now