Search code examples
xmlvb.netxamlcheckboxcontrols

Is there a way to dynamically name/control checkboxes in visual basic?


I have xaml code that creates a list view with two columns; the first column is checkboxes based on the number of items that are in the second column. The issue with the way the code is written is the checkboxes do not have their own names (as far as I can tell). If i rename the object then all 15 of the checkboxes will be named the same thing not allowing me control over what happens if any one is checked (I think). Is there a way I can code it to where they have independent control from one another?

This is what I used to create this far

<Grid>
   <ListView Grid.Row="2" Grid.Column="2" ItemsSource="{Binding XPath=..//@ParentAttribute}">
      <ListView.View>
         <GridView>
            <GridViewColumn Header="Selection">
               <GridViewColumn.CellTemplate>
                   <DataTemplate>
                      <CheckBox Name="Check"/>
                   </DataTemplate>
               </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn>
            </GridViewColumn>
         </GridView>
      </ListView.View>
   </ListView>
</Grid>

Solution

  • You can add an event handler to your CheckBox:

    <DataTemplate>
        <CheckBox Name="Check"
                    Checked="Check_Checked" />
    </DataTemplate>
    

    In your code-behind, this will create a Check_Checked event handler:

    Private Sub Check_Checked(sender As Object, e As RoutedEventArgs)
    
        If e.Source.GetType = GetType(CheckBox) Then
    
            Dim checkBox As CheckBox = CType(e.Source, CheckBox)
            Dim xmlElement As XmlElement = CType(checkBox.DataContext, XmlElement)
    
            MsgBox(xmlElement.OuterXml)
    
        End If
    
    End Sub
    

    One this event is raised, the DataContext of the CheckBox that was checked will be an XmlElement. From this you should be able to retrieve anything you need from the item that was clicked.

    Here's a simple WPF Window I built with XML I got from this Microsoft example:

    <Window.Resources>
    
        <XmlDataProvider x:Key="InventoryData"
                         XPath="Inventory/Books">
            <x:XData>
                <Inventory xmlns="">
                    <Books>
                        <Book ISBN="0-7356-0562-9"
                              Stock="in"
                              Number="9">
                            <Title>XML in Action</Title>
                            <Summary>XML Web Technology</Summary>
                        </Book>
                        <Book ISBN="0-7356-1370-2"
                              Stock="in"
                              Number="8">
                            <Title>Programming Microsoft Windows With C#</Title>
                            <Summary>C# Programming using the .NET Framework</Summary>
                        </Book>
                        <Book ISBN="0-7356-1288-9"
                              Stock="out"
                              Number="7">
                            <Title>Inside C#</Title>
                            <Summary>C# Language Programming</Summary>
                        </Book>
                        <Book ISBN="0-7356-1377-X"
                              Stock="in"
                              Number="5">
                            <Title>Introducing Microsoft .NET</Title>
                            <Summary>Overview of .NET Technology</Summary>
                        </Book>
                        <Book ISBN="0-7356-1448-2"
                              Stock="out"
                              Number="4">
                            <Title>Microsoft C# Language Specifications</Title>
                            <Summary>The C# language definition</Summary>
                        </Book>
                    </Books>
                    <CDs>
                        <CD Stock="in"
                            Number="3">
                            <Title>Classical Collection</Title>
                            <Summary>Classical Music</Summary>
                        </CD>
                        <CD Stock="out"
                            Number="9">
                            <Title>Jazz Collection</Title>
                            <Summary>Jazz Music</Summary>
                        </CD>
                    </CDs>
                </Inventory>
            </x:XData>
        </XmlDataProvider>
    
    </Window.Resources>
    
    <Grid>
    
        <Grid>
    
            <ListView Grid.Row="2"
                      Grid.Column="2">
                <ListView.ItemsSource>
                    <Binding Source="{StaticResource InventoryData}"
                             XPath="*[@Stock='out'] | *[@Number>=8 or @Number=3]" />
                </ListView.ItemsSource>
    
                <ListView.View>
    
                    <GridView>
    
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock FontSize="12"
                                               Foreground="Red">
                                        <TextBlock.Text>
                                            <Binding XPath="Title" />
                                        </TextBlock.Text>
                                    </TextBlock>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
    
                        <GridViewColumn Header="Selection">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox Name="Check"
                                              Checked="Check_Checked" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
    
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    
    </Grid>