【问题标题】:I want to get the text of the listview that I clicked on我想获取我单击的列表视图的文本
【发布时间】:2017-04-10 10:07:22
【问题描述】:
<Window.Resources>
    <Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
    </Style>
</Window.Resources>
<Grid>
    <TabControl x:Name="tabControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <TabItem Header="업무공지">
            <Grid Background="#FFE5E5E5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <ListView Name="listView" 
                                Grid.Row="1"   
                                ItemContainerStyle="{StaticResource itemstyle}"
                                Margin="4" 
                                Padding="2"

                                SelectionMode="Single">
                    <ListView.View>
                        <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
                            <GridViewColumn >
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate >
                                        <CheckBox IsChecked="{Binding Finished}" HorizontalAlignment="Center" />
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Description}" Width="340" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>

以上代码是我的xaml代码。

接下来,代码隐藏点击事件


private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var item = sender as ListViewItem;

    if (item != null && item.IsSelected)
    {
        MessageBox.Show(item.Content.ToString());
    }
}

我不想看到控件名称。

我想看到的是“案件已经结束”。成为。

如何获取我单击的列表视图单元格中的文本?

【问题讨论】:

    标签: wpf


    【解决方案1】:

    目前尚不清楚“案件已结束”文本的来源,但您可以尝试将 Content 转换为 Task 并访问其任何属性:

    private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var item = sender as ListViewItem;
        if (item != null && item.IsSelected)
        {
            var task = item.Content as Copsys.Comm.Messenger.Task;
            if (task != null)
            {
                MessageBox.Show(task.Description);
            }
        }
    }
    

    【讨论】:

    • 我在这里看到的另一种方法是使用 SelectedItem 属性。
    • @NareshRavlani 我想看看另一种方式。我是一个充满激情的新手开发人员。打扰一下,你能告诉我另一种方式吗?
    • @mm8 你真的很擅长开发。怎样才能像你一样发展好?
    • @KangDongGyun 绑定 ListView 的 Selected Item 属性。它将是 Copsys.Comm.Messenger.Task 类型。然后您可以访问 Copsys.Comm.Messenger.Task 的任何属性您可以在 stackoverflow.com/questions/1402878/… 上找到相同的代码示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    相关资源
    最近更新 更多