SuxoiKorm
SuxoiKorm

Reputation: 179

Checkbox in ItemTemplate

I have a <Checkbox/> in my <GridView.ItemTemplate>. How do I handle the <Checkbox/> as to the element in which it is?

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:

<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>

Upvotes: 0

Views: 374

Answers (1)

Chiune Sugihara
Chiune Sugihara

Reputation: 1229

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. 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.

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). You should be able to solve your issue this way.

Upvotes: 1

Related Questions