简体   繁体   English

如何从列表视图中查看一个项目,当我单击列表视图的该项目时,它将在文本块上显示数据?

[英]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在文本块上将 Text 属性绑定到 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.其中“SomeProperty”是数据库模型中的一列。

You can just get the selected item's date from item click event of ListView.您可以从 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.还要使 Listview 的 IsItemClickEnabled="True" 。 Now you can give text of textblock as _date or assign the datacontext of that textblock as item.现在您可以将文本块的文本指定为 _date 或将该文本块的数据上下文指定为项目。

The most basic implementation would be to implement an event handler for the ListView's SelectedIndexChanged event.最基本的实现是为 ListView 的SelectedIndexChanged事件实现一个事件处理程序。 And in the event handler just change the textblock text property to the value of the SelectedItem property you want to show.在事件处理程序中,只需将 textblock 文本属性更改为要显示的SelectedItem属性的值。

A better approach would to use XAML databinding and bind the Text property of the TextBox to the SelectedItem property you want to show:更好的方法是使用XAML数据绑定并将TextBoxText属性绑定到要显示的SelectedItem属性:

Further reading:进一步阅读:

Quickstart: Data binding to controls (XAML) 快速入门:数据绑定到控件 (XAML)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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