如何判断 ListViewItem 何时获得焦点?
How do I tell when a ListViewItem gets focus?
我有一个 UWP XAML ListView,我想在使用箭头键在项目之间切换时处理焦点事件。但是,我不知道如何处理我的项目的焦点事件:
<ListView ItemsSource="{x:Bind Items}"
CanDragItems="True" CanReorderItems="True" AllowDrop="True"
SelectionMode="None" IsItemClickEnabled="True" ItemClick="ListView_ItemClick">
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<!-- Never fires, even with Control.IsTemplateFocusTarget="True" : -->
<StackPanel GotFocus="StackPanel_GotFocus">
<!-- Never fires: -->
<TextBlock Text="{x:Bind}" GotFocus="TextBlock_GotFocus" />
<Button Content="Foo" IsTabStop="False" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<!-- Never fires: -->
<ListViewItemPresenter [...]
GotFocus="Root_GotFocus" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
我还能在哪里收听这些焦点事件?
谢谢!
使用 <ListView>
或 <ListBox>
时,您不需要使用 GotFocus
事件。
相反,您在主 <listView>
控件中使用 SelectionChanged
事件,并在代码中获取已选择的 <ListViewItem>
的索引。
SelectionChanged
事件在用户每次在 <ListView>
中更改选择时触发。
ListView.SelectedIndex
returns 选中的索引号 <ListViewItem>
第一项为0.
这是一个例子:
XAML:
<Image x:Name="img"/>
<ListView x:Name="listView" SelectionChanged="ListView_SelectionChanged">
<ListViewItem>Image 1</ListViewItem>
<ListViewItem>Image 2</ListViewItem>
<ListViewItem>Image 3</ListViewItem>
</ListView>
C#:
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int num = listView.SelectedIndex + 1;
img.Source = new BitmapImage(new Uri($"ms-appx:///Assets/Pictures/image{num}.jpg"));
}
我有一个 UWP XAML ListView,我想在使用箭头键在项目之间切换时处理焦点事件。但是,我不知道如何处理我的项目的焦点事件:
<ListView ItemsSource="{x:Bind Items}"
CanDragItems="True" CanReorderItems="True" AllowDrop="True"
SelectionMode="None" IsItemClickEnabled="True" ItemClick="ListView_ItemClick">
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<!-- Never fires, even with Control.IsTemplateFocusTarget="True" : -->
<StackPanel GotFocus="StackPanel_GotFocus">
<!-- Never fires: -->
<TextBlock Text="{x:Bind}" GotFocus="TextBlock_GotFocus" />
<Button Content="Foo" IsTabStop="False" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<!-- Never fires: -->
<ListViewItemPresenter [...]
GotFocus="Root_GotFocus" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
我还能在哪里收听这些焦点事件?
谢谢!
使用 <ListView>
或 <ListBox>
时,您不需要使用 GotFocus
事件。
相反,您在主 <listView>
控件中使用 SelectionChanged
事件,并在代码中获取已选择的 <ListViewItem>
的索引。
SelectionChanged
事件在用户每次在 <ListView>
中更改选择时触发。
ListView.SelectedIndex
returns 选中的索引号 <ListViewItem>
第一项为0.
这是一个例子:
XAML:
<Image x:Name="img"/>
<ListView x:Name="listView" SelectionChanged="ListView_SelectionChanged">
<ListViewItem>Image 1</ListViewItem>
<ListViewItem>Image 2</ListViewItem>
<ListViewItem>Image 3</ListViewItem>
</ListView>
C#:
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int num = listView.SelectedIndex + 1;
img.Source = new BitmapImage(new Uri($"ms-appx:///Assets/Pictures/image{num}.jpg"));
}