简体   繁体   中英

How can i view an item from a listview that when I click on that item of the listview it will show data on a textblock?

我有一个 sqlite 数据库,我使用列表视图来显示该数据库。所以我需要知道如何实现,当我从列表视图中单击一个项目时,数据库中的某个日期将显示在文本块上。

On the textblock bind the Text property to the listview.SelectedItem.SomeProperty

<Window
    DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}">

    <Grid>
        <ListView x:Name="listview"/>
        <TextBlock Text="{Binding SelectedItem.SomeProperty, ElementName=listview}"/>
    </Grid>
<Window>

Where "SomeProperty" is a column in your database model.

You can just get the selected item's date from item click event of ListView.

private void MainListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var item = e.ClickedItem as SampleClass;
            var _date = item.ItemDate;
        }

Also make IsItemClickEnabled="True" of your Listview. Now you can give text of textblock as _date or assign the datacontext of that textblock as item.

The most basic implementation would be to implement an event handler for the ListView's SelectedIndexChanged event. And in the event handler just change the textblock text property to the value of the SelectedItem property you want to show.

A better approach would to use XAML databinding and bind the Text property of the TextBox to the SelectedItem property you want to show:

Further reading:

Quickstart: Data binding to controls (XAML)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM