在wpf xml中找到选定的项目

时间:2019-02-17 15:25:54

标签: c# xml wpf

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

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

并使用此代码在messsagebox中显示节目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());
    }

现在我将断点放置在此函数中,但它未输入此函数。有什么问题吗?

1 个答案:

答案 0 :(得分: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>

每当选择一个新项目时,它将被引发。