-2

我有这个 xml 代码用于在 wpf 中显示列表视图项:

 <ListView FlowDirection="RightToLeft"  Name="ListViewPost" HorizontalAlignment="Left" Height="504" Margin="1060,172,0,0" VerticalAlignment="Top" Width="304" Background="White" BorderBrush="Black">
        <ListView.View >
            <GridView>
                <GridViewColumn Width="300" Header="عنوان" DisplayMemberBinding="{Binding Title}"/>
            </GridView>
        </ListView.View>
    </ListView>

并使用此代码在 messagebox 中显示 id:

    private void listView1_MouseClick_1(object sender, RoutedEventArgs e)
    {
        int Id;
        if (ListViewPost.SelectedIndex == -1) return;

        Id = (int)ListViewPost.SelectedItems[0];
        MessageBox.Show(Id.ToString());
    }

现在我把断点放在这个函数中,但它没有进入这个。有什么问题 ?

4

1 回答 1

0

您可以处理该SelectionChanged事件:

private void ListViewPost_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    int Id;
    if (ListViewPost.SelectedIndex == -1) return;

    Id = (int)ListViewPost.SelectedItems[0];
    MessageBox.Show(Id.ToString());
}

XAML:

<ListView FlowDirection="RightToLeft"  Name="ListViewPost" HorizontalAlignment="Left" Height="504"
          VerticalAlignment="Top" Width="304" Background="White" BorderBrush="Black"
          SelectionChanged="ListViewPost_SelectionChanged_1">
    <ListView.View >
        <GridView>
            <GridViewColumn Width="300" Header="عنوان" DisplayMemberBinding="{Binding Title}"/>
        </GridView>
    </ListView.View>
</ListView>

It will be raised whenever a new item is selected.

于 2019-02-18T14:43:09.547 回答