C# 如何使用DataTemplate访问列表框中的特定项

C# 如何使用DataTemplate访问列表框中的特定项,c#,windows-phone-8.1,C#,Windows Phone 8.1,我使用的是Telerik,HubSection与WP8类似 <HubSection x:Uid="Section4Header" Header="All note" > <DataTemplate> <ListBox x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row

我使用的是Telerik,HubSection与WP8类似

<HubSection x:Uid="Section4Header" Header="All note" >
    <DataTemplate>
        <ListBox x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1"  SelectionChanged="listBoxobj_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="350" >
                        <Border Margin="5" BorderBrush="White" BorderThickness="1">
                            <Grid Holding="Grid_Holding" VerticalAlignment="Stretch">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <FlyoutBase.AttachedFlyout>
                                    <MenuFlyout>
                                        <MenuFlyoutItem x:Name="EditButton"
                                            Text="Export To PDF"
                                            Click="EditButton_Click"
                                        />
                                        <MenuFlyoutItem x:Name="EditButton1"
                                            Text="Export To PDF syncfu"
                                            Click="EditButton1_Click"
                                        />
                                    </MenuFlyout>
                                </FlyoutBase.AttachedFlyout>
                                <TextBlock Margin="5,0,0,0" Grid.Row="0" x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding Name}" FontSize="28" Foreground="White"/>
                                <TextBlock HorizontalAlignment="Right" Margin="0,0,35,0" Grid.Row="3" x:Name="CreateddateTxt" Foreground="White" FontSize="18" TextWrapping="Wrap" Text="{Binding CreationDate}" />
                            </Grid>
                        </Border>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </DataTemplate>
</HubSection>
listBoxobj是ListBox的名称 问题是如何访问列表框

当前上下文中不存在名称“listBoxobj”


您将无法直接执行此操作,因为它位于DataTemplate中,因此您的列表框是在运行时生成的

按照本文中的步骤将ListBox从可视树中拉出,您可能会感到高兴:

正如Mike所说,您将无法从中心代码直接访问列表框,因为它位于数据模板中

但是,您可以创建一个用户控件,例如,
MyUserControl1
。 将列表框放入
MyUserControl1.xaml
文件中,并将其相关的c代码放入
MyUserControl1.cs
文件中。 然后,将用户控件添加到您的区域中

<HubSection x:Uid="Section4Header" Header="All note" >
    <DataTemplate>
        <local:MyUserControl1 x:Name="ListControl"/>
    </DataTemplate>
</HubSection>

编辑:将列表框代码放入用户控件代码中:

<ListBox x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1"  SelectionChanged="listBoxobj_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="350" >
                    <Border Margin="5" BorderBrush="White" BorderThickness="1">
                        <Grid Holding="Grid_Holding" VerticalAlignment="Stretch">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <FlyoutBase.AttachedFlyout>
                                <MenuFlyout>
                                    <MenuFlyoutItem x:Name="EditButton"
                                        Text="Export To PDF"
                                        Click="EditButton_Click"
                                    />
                                    <MenuFlyoutItem x:Name="EditButton1"
                                        Text="Export To PDF syncfu"
                                        Click="EditButton1_Click"
                                    />
                                </MenuFlyout>
                            </FlyoutBase.AttachedFlyout>
                            <TextBlock Margin="5,0,0,0" Grid.Row="0" x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding Name}" FontSize="28" Foreground="White"/>
                            <TextBlock HorizontalAlignment="Right" Margin="0,0,35,0" Grid.Row="3" x:Name="CreateddateTxt" Foreground="White" FontSize="18" TextWrapping="Wrap" Text="{Binding CreationDate}" />
                        </Grid>
                    </Border>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>


你能举个例子吗?它已经在那里了!然而,请阅读后面的部分。现在我已经添加了完整的代码。要创建UserControl,请在解决方案资源管理器中的项目上单击鼠标右键,选择“添加”,然后选择“UserControl”。
<ListBox x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1"  SelectionChanged="listBoxobj_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="350" >
                    <Border Margin="5" BorderBrush="White" BorderThickness="1">
                        <Grid Holding="Grid_Holding" VerticalAlignment="Stretch">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <FlyoutBase.AttachedFlyout>
                                <MenuFlyout>
                                    <MenuFlyoutItem x:Name="EditButton"
                                        Text="Export To PDF"
                                        Click="EditButton_Click"
                                    />
                                    <MenuFlyoutItem x:Name="EditButton1"
                                        Text="Export To PDF syncfu"
                                        Click="EditButton1_Click"
                                    />
                                </MenuFlyout>
                            </FlyoutBase.AttachedFlyout>
                            <TextBlock Margin="5,0,0,0" Grid.Row="0" x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding Name}" FontSize="28" Foreground="White"/>
                            <TextBlock HorizontalAlignment="Right" Margin="0,0,35,0" Grid.Row="3" x:Name="CreateddateTxt" Foreground="White" FontSize="18" TextWrapping="Wrap" Text="{Binding CreationDate}" />
                        </Grid>
                    </Border>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>