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

x
  • 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
0
ALawrence007
Asked:
ALawrence007
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:


Window1.xaml.vb
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
        StackPanel1.Children.Add(uc)
    End Sub
End Class
 
-- Window1.xaml
<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <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>
    </Grid>
</Window>
 
-- TestControl.xaml
<UserControl x:Class="TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="200" Height="200">
    <Rectangle Height="200" Name="Rectangle1" Stroke="Black" Fill="LimeGreen" Width="200" />
</UserControl>

Open in new window

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

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