通过单击其DataTemplate中的控件来选择ListViewItem

时间:2022-10-16 22:57:55

I've written a custom DataTemplate for items in a ListView, something like this:

我为ListView中的项目编写了一个自定义DataTemplate,如下所示:

 <DataTemplate x:Key="CustomerStateTemplate">
    <Grid Margin="5, 5, 5, 5">
        <Grid.ColumnDefinitions>
            ...
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            ...
        </Grid.RowDefinitions>

        <Image Grid.Row="0" Grid.RowSpan="2" Width="24" Height="20" ... />

        <TextBox Style="{StaticResource CustomerStyle}" Grid.Column="0" 
                       Grid.Row="0" Grid.ColumnSpan="2"
                       Name="nameField">
            <TextBox.Text>
                <Binding Path="Name" />
            </TextBox.Text>
        </TextBox>

        ...

and I've obtain my pretty style. Now, if I want to select the item, I must click on the white space between the template controls. If I click on the textbox in the ListViewItem, it won't select like an item. So, is there a way to select a ListViewItem by clicking on the controls in its template?

我得到了我漂亮的风格。现在,如果我想选择项目,我必须单击模板控件之间的空白区域。如果我单击ListViewItem中的文本框,它将不会像项目一样选择。那么,有没有办法通过单击其模板中的控件来选择ListViewItem?

Thanks thousand!

万分感谢!

1 个解决方案

#1


4  

Its possible to add a trigger on the ListViewItem which selects the item always then the keyboardfocus is inside of the item. As you do that on the ListViewItem you have the same behavior for all the controls inside the DataTemplate, which should be your solution...

可以在ListViewItem上添加一个触发器来选择项目,然后键盘焦点就在项目内部。当您在ListViewItem上执行此操作时,您对DataTemplate中的所有控件具有相同的行为,这应该是您的解决方案...

example:

例:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <Grid>
  <Grid.Resources>
    <x:Array x:Key="Data" Type="{x:Type sys:String}">
      <sys:String>first</sys:String>
      <sys:String>second</sys:String>
      <sys:String>third</sys:String>
    </x:Array>
  <Style TargetType="ListViewItem" x:Key="itemStyle">
    <Style.Triggers>
      <Trigger Property="IsKeyboardFocusWithin" Value="True">
        <Setter Property="IsSelected" Value="True" />
      </Trigger>
    </Style.Triggers>
  </Style>
  </Grid.Resources>


  <ListView ItemsSource="{StaticResource Data}" ItemContainerStyle="{StaticResource itemStyle}">
    <ListView.ItemTemplate>
      <DataTemplate DataType="{x:Type sys:String}">
        <TextBox Text="{Binding .}">
        </TextBox>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
  </Grid>
</Page>

i hope its clear...

我希望它清楚......

#1


4  

Its possible to add a trigger on the ListViewItem which selects the item always then the keyboardfocus is inside of the item. As you do that on the ListViewItem you have the same behavior for all the controls inside the DataTemplate, which should be your solution...

可以在ListViewItem上添加一个触发器来选择项目,然后键盘焦点就在项目内部。当您在ListViewItem上执行此操作时,您对DataTemplate中的所有控件具有相同的行为,这应该是您的解决方案...

example:

例:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <Grid>
  <Grid.Resources>
    <x:Array x:Key="Data" Type="{x:Type sys:String}">
      <sys:String>first</sys:String>
      <sys:String>second</sys:String>
      <sys:String>third</sys:String>
    </x:Array>
  <Style TargetType="ListViewItem" x:Key="itemStyle">
    <Style.Triggers>
      <Trigger Property="IsKeyboardFocusWithin" Value="True">
        <Setter Property="IsSelected" Value="True" />
      </Trigger>
    </Style.Triggers>
  </Style>
  </Grid.Resources>


  <ListView ItemsSource="{StaticResource Data}" ItemContainerStyle="{StaticResource itemStyle}">
    <ListView.ItemTemplate>
      <DataTemplate DataType="{x:Type sys:String}">
        <TextBox Text="{Binding .}">
        </TextBox>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
  </Grid>
</Page>

i hope its clear...

我希望它清楚......