gpt4 book ai didi

c# - 通过单击其DataTemplate中的控件来选择一个ListViewItem

转载 作者:行者123 更新时间:2023-12-03 19:16:08 25 4
gpt4 key购买 nike

我已经为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>

...


而且我已经获得了漂亮的风格。
现在,如果要选择该项,则必须单击模板控件之间的空白。如果单击ListViewItem中的文本框,它将不会像项目一样选择。因此,有没有一种方法可以通过单击其模板中的控件来选择ListViewItem?

谢谢千!

最佳答案

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

例:



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


我希望它清楚...

关于c# - 通过单击其DataTemplate中的控件来选择一个ListViewItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6264089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com