简体   繁体   中英

How to get listview datatemplate item doubleclicked WPF MVVM

How can I bind to a datatemplate which is show in a listview and is being doubleclicked?

I want smthing like this:

<CheckBox Grid.Column="1" Grid.Row="5" Command="{Binding AddItemCommand}"></CheckBox>

I already have everything done, but don't know how to bind a command to a datatemplate in listview or to a Grid in that datatemplate.

Datatempalte is like this:

<DataTemplate x:Key="ServerViewItemTemplate" DataType="ViewModel:ServerViewModel">
        <Border BorderBrush="Black" BorderThickness="1" Margin="10">

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="70" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="20" />
                    <RowDefinition Height="20" />
                    <RowDefinition Height="20" />
                    <RowDefinition Height="20" />
                    <RowDefinition Height="20" />
                    <RowDefinition Height="20" />
                </Grid.RowDefinitions>

                <TextBlock Grid.Column="0" Grid.Row="0" Text="ServerId:" FontWeight="Bold"/>
                <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding ServerId}" FontWeight="Bold"/>
                <TextBlock Grid.Column="0" Grid.Row="1" Text="Login:"/>
                <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Login}" />
                <TextBlock Grid.Column="0" Grid.Row="2" Text="Password:"/>
                <TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding Password}" />
                <TextBlock Grid.Column="0" Grid.Row="3" Text="Type:"/>
                <TextBlock Grid.Column="1" Grid.Row="3" Text="{Binding Type}" />
                <TextBlock Grid.Column="0" Grid.Row="4" Text="IpAddress:"/>
                <TextBlock Grid.Column="1" Grid.Row="4" Text="{Binding IpAddress}" />
                <TextBlock Grid.Column="0" Grid.Row="5" Text="Include:"/>
                <CheckBox Grid.Column="1" Grid.Row="5" Command="{Binding AddItemCommand}"></CheckBox>
            </Grid>
        </Border>
    </DataTemplate>

So if it is double-clicked it should call the ICommand, the same as in the CheckBox AddItemCommand.

I'd suggest a Behavior to handle the double click event. Depending on what you want to do when you double click, you may need to add an ICommand Dependency Property to the Behavior so you can bind to a command on your viewmodel.

Here's a similar post: Is there a way in XAML to select all text in textbox when double clicked?

In that example, the user is selecting text in a TextBox when double clicked.

MORE:

Your XAML will look something like this:

<DataTemplate x:Key="ServerViewItemTemplate" DataType="ViewModel:ServerViewModel">
    <Border BorderBrush="Black" BorderThickness="1" Margin="10">

        <Grid>
            <e.Interaction.Behaviors>
                <local:DoubleClickBehavior Command="{Binding Path=AddItemCommand}"/>
            </e.Interaction.Behaviors>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="70" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
... ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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