Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows runtime 在WinRT中,如何获取listviewitem中按钮的父级?_Windows Runtime - Fatal编程技术网

Windows runtime 在WinRT中,如何获取listviewitem中按钮的父级?

Windows runtime 在WinRT中,如何获取listviewitem中按钮的父级?,windows-runtime,Windows Runtime,我想获取按钮的ListViewItem。在正常的WPF中,我会使用GetContainerForItem(),但找不到WinRT的等效项。在我的AddButton_Click()函数中,您将看到我获取按下关联按钮的项目id的脆弱方式 <DataTemplate x:Key="Custom80ItemTemplate"> <Grid Margin="6"> <Grid.ColumnDefinitions>

我想获取按钮的ListViewItem。在正常的WPF中,我会使用GetContainerForItem(),但找不到WinRT的等效项。在我的AddButton_Click()函数中,您将看到我获取按下关联按钮的项目id的脆弱方式

<DataTemplate x:Key="Custom80ItemTemplate">
        <Grid Margin="6">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" Width="40" Height="40" Click="AddButton_Click" Content="&#xE118;" FontFamily="Segoe UI Symbol" Style="{StaticResource TextButtonStyle}" />
            <StackPanel Grid.Column="1" Margin="10,0,0,0">
                <TextBlock Text="{Binding name}" Style="{StaticResource ItemTextStyle}" MaxHeight="40"/>
                <TextBlock Text="{Binding id}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/>
                <TextBlock Text="{Binding group}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/>
            </StackPanel>
        </Grid>
    </DataTemplate>


    <ListView x:Name="searchResultsListView" Grid.Row="1"
              SelectionMode="None" 
              HorizontalAlignment="Left" 
              Margin="10,0,10,10" 
              ItemTemplate="{StaticResource Custom80ItemTemplate}" />

    private async void AddButton_Click(object sender, RoutedEventArgs e)
    {
        var parent1 = ((FrameworkElement)sender).Parent;
        var grid = parent1 as Grid;
        var stackpanel = grid.Children[1] as StackPanel;
        var textBlock = stackpanel.Children[1] as TextBlock;
     }

私有异步void AddButton\u单击(对象发送方,路由目标)
{
var parent1=((FrameworkElement)sender.Parent;
var grid=parent1作为网格;
var stackpanel=grid.Children[1]作为stackpanel;
var textBlock=stackpanel.Children[1]作为textBlock;
}

为什么需要直接访问模板中的元素?如果您只想访问所单击项目的
id
值,您可以将按钮的命令放在项目本身中:

public class ItemViewModel
{
    public string name { get; set; }
    public string id { get; set; }
    public string group { get; set; }

    public ICommand command { get; set; }

    public ItemViewModel
    {
        command = new RelayCommand(_ => OnClick());
    }

    private void OnClick()
    {
        // access id here
    }
}
您可以找到一个
RelayCommand
实现

现在只需通过绑定
命令
替换事件处理程序:

<Button Grid.Column="0" Width="40" Height="40" Command="{Binding command}" Content="&#xE118;" FontFamily="Segoe UI Symbol" Style="{StaticResource TextButtonStyle}" />

哇,这太明显了。:)这就是我一直在寻找的答案。