【问题标题】:find selected item in wpf xml在 wpf xml 中查找所选项目
【发布时间】:2019-02-17 15:25:54
【问题描述】:

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

并使用此代码在消息框中显示 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());
    }

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

【问题讨论】:

  • 因为您没有连接事件。换句话说,您还没有将 listView1_MouseClick_1 处理程序订阅到相应的 ListView 事件。哦,顺便说一句,ListView 本身没有 Click 事件。布玛!即使您正在设法执行类似的操作(例如使用 MouseDown/Up 事件,也许),单击 ListView in 中的项目与单击 ListView 不同。您可能已经想到,尝试处理 ListView 鼠标点击可能无助于实现您的目标(无论它是什么)。
  • 现在,先问问自己究竟你想点击什么来显示消息框?
  • @elgonzo 是的,我需要这个
  • @elgonzo 请告诉我如何显示该 ID?
  • 您的问题目前无法回答。我在第二条评论中已经说过你需要先自己弄清楚什么,这将成为你如何编写代码以实现目标的起点。请不要让我为你设计和编写你的程序......

标签: c# xml wpf


【解决方案1】:

您可以处理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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 2012-09-25
    • 1970-01-01
    相关资源
    最近更新 更多