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

The above code is my xaml code.

Next, code-behind click event


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

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

enter image description here

I do not want to see the control name.

What I want to see is "The case has ended." to be.

How can I get the text in the listview cell that I clicked on?

mm8
  • 163,881
  • 10
  • 57
  • 88

1 Answers1

1

It is unclear where the text "The case has ended" is supposed to come from but you could try to cast the Content to a Task and access any of its properties:

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);
        }
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Another way I see here is to use SelectedItem property. – Naresh Ravlani Apr 10 '17 at 10:33
  • @NareshRavlani I would like to see another way. I am a novice developer full of passion. Excuse me, can you show me another way? – Kang Dong Gyun Apr 10 '17 at 10:40
  • @mm8 You are really good at development. How can you develop well like you? – Kang Dong Gyun Apr 10 '17 at 10:46
  • @KangDongGyun Bind a Selected Item property of ListView. It will be of type Copsys.Comm.Messenger.Task. Then you can access any property of Copsys.Comm.Messenger.Task You can find code sample for same on http://stackoverflow.com/questions/1402878/wpf-listview-access-to-selecteditem-and-subitems – Naresh Ravlani Apr 10 '17 at 11:05