简体   繁体   English

ItemTemplate中的复选框

[英]Checkbox in ItemTemplate

I have a <Checkbox/> in my <GridView.ItemTemplate> . 我的<GridView.ItemTemplate>有一个<Checkbox/> <GridView.ItemTemplate> How do I handle the <Checkbox/> as to the element in which it is? 如何处理<Checkbox/>的元素?

For example , I want to delete item when checkbox checked. 例如 ,我要在选中复选框时删除项目。

I think should write here. 我认为应该在这里写。 But what? 但是呢

private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
{

}

Here's My XAML: 这是我的XAML:

<GridView Margin="0,10,0,0" 
        RelativePanel.AlignHorizontalCenterWithPanel="True"
        x:Name="GridColections"
        IsItemClickEnabled="True"
        SelectionMode="None"
        ItemsSource="{x:Bind DS.AllRem, Mode=OneWay}"
        ItemClick="GridColections_ItemClick" >
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="local:GetRem" >
            <Grid Margin="-2,0,-6,0" BorderBrush="LightGray" BorderThickness="1" HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="40" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30" />
                    <RowDefinition Height="30" />
                </Grid.RowDefinitions>
                <TextBlock TextTrimming="CharacterEllipsis" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Stretch" TextWrapping="Wrap" Text="{x:Bind ReminderName}" Margin="5,5,0,0" FontSize="20"/>
                <TextBlock TextTrimming="CharacterEllipsis" Grid.Column="0" Grid.Row="1" Width="600" TextWrapping="Wrap" Text="{x:Bind ReminderDescription}" Margin="5,5,0,0" FontSize="12"/>
                <CheckBox Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" VerticalAlignment="Center" Checked="CheckBox_Checked_1"/>
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

The problem is that you almost certainly want to be able to use the DataContext in your click handler but you won't get that easily by just having a reference to the CheckBox which will be the sender argument in your callback. 问题是,您几乎肯定希望能够在单击处理程序中使用DataContext ,但是仅通过引用CheckBox (将作为回调中的sender参数)就不会那么容易。 Normally what you would do here is create a Command on your item's view model and bind to that and any additional information that you would want to pass in you would pass in through the CheckBox 's CommandParameter . 通常,您将在项目的视图模型上创建一个Command并绑定到该模型,您想要传递的任何其他信息都将通过CheckBoxCommandParameter传递。

Once you do this you are now operating in your view model with a reference to whatever piece of information that you need through the command parameter (for instance you could set CommandParameter = "{Binding}" to pick up the entire data context which would be the item's view model and that would be accessible from your Command as an argument to it). 完成此操作后,您现在可以在视图模型中进行操作,并通过命令参数引用所需的任何信息(例如,您可以将CommandParameter = "{Binding}"为获取整个数据上下文)该项目的视图模型,并且可以从您的Command将该模型作为其参数进行访问)。 You should be able to solve your issue this way. 您应该可以通过这种方式解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM