[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1826
  • Last Modified:

Display Custom WPF User Control using Code

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
1 Solution
Jeff CertainCommented:
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="http://schemas.microsoft.com/winfx/2006/xaml" Width="200" Height="200">
    <Rectangle Height="200" Name="Rectangle1" Stroke="Black" Fill="LimeGreen" Width="200" />

Open in new window

ALawrence007Author Commented:
Thanks Chaosian, this helped me find my answer.

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Tackle projects and never again get stuck behind a technical roadblock.
Join Now