有没有办法在 Visual Basic 中动态命名/控制复选框?

问题描述 投票:0回答:1

我有创建包含两列的列表视图的 xaml 代码;第一列是基于第二列中项目数的复选框。代码编写方式的问题是复选框没有自己的名称(据我所知)。如果我重命名该对象,那么所有 15 个复选框将被命名为相同的东西,不允许我控制如果选中任何一个(我认为)会发生什么。有没有一种方法可以将它编码到他们可以相互独立控制的地方?

这就是我用来创建到目前为止的

<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>
xml vb.net xaml checkbox controls
1个回答
0
投票

您可以将事件处理程序添加到您的 CheckBox:

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

在您的代码隐藏中,这将创建一个

Check_Checked
事件处理程序:

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

一旦引发此事件,选中的 CheckBox 的 DataContext 将是一个 XmlElement。从这里你应该能够从被点击的项目中检索你需要的任何东西。

这是我用 XML 构建的简单 WPF 窗口,我从 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>
© www.soinside.com 2019 - 2024. All rights reserved.