【问题标题】:How do I tell when a ListViewItem gets focus?如何判断 ListViewItem 何时获得焦点?
【发布时间】:2021-03-04 01:05:49
【问题描述】:

我有一个 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>

我还能在哪里收听这些焦点事件?

谢谢!

【问题讨论】:

    标签: xaml uwp windows-runtime


    【解决方案1】:

    当使用&lt;ListView&gt;&lt;ListBox&gt; 时,您不需要使用GotFocus 事件。 相反,您在主&lt;listView&gt; 控件中使用SelectionChanged 事件,并在代码中获取被选中的&lt;ListViewItem&gt; 的索引。

    每次用户在&lt;ListView&gt;中更改选择时,都会触发SelectionChanged事件。

    ListView.SelectedIndex返回选中的&lt;ListViewItem&gt;的索引号第一项为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"));
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-29
      • 2012-01-11
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多