Retrieve Cell Value On MouseEnter In A Multiple Column ListView With Data Binding

后端 未结 1 494
执笔经年
执笔经年 2021-01-29 02:44

I would like to retrieve the value from a Car Name column from a row in a ListView on a MouseEnter event and have the result display in a TextBlock.

相关标签:
1条回答
  • 2021-01-29 03:12

    Try this:

    xaml:

    <Grid>
        <ListView x:Name="carList" VerticalAlignment="Top" MouseEnter="carList_MouseEnter">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="MouseEnter" Handler="ListBoxItem_MouseEnter" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Car" DisplayMemberBinding="{Binding Name}" Width="Auto"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Make}" Header="Make" Width="Auto"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Year}" Header="Year" Width="Auto"/>
                </GridView>
            </ListView.View>
        </ListView>
        <TextBox x:Name="textbox" Height="23" Margin="5,0,0,5" TextWrapping="Wrap" VerticalAlignment="Bottom"/>
    </Grid>
    

    cs:

    private void ListBoxItem_MouseEnter(object s, MouseEventArgs e)
    {
        var item = e.OriginalSource as ListBoxItem;
        var car = item.Content as Car;
        textbox.Text = car.Name;
    }
    
    0 讨论(0)
提交回复
热议问题