listview 完成对数据的绑定,已经显示出一个表格。
现在我要单击某一行,有4列。现在我有4个textbox,我想把数据动态的绑定到textbox里。
怎么实现。比如。单击行数据为 123,张三,政治,2008-12-12分别把这4个数据显示到对应的textbox里。
现在我要单击某一行,有4列。现在我有4个textbox,我想把数据动态的绑定到textbox里。
怎么实现。比如。单击行数据为 123,张三,政治,2008-12-12分别把这4个数据显示到对应的textbox里。
解决方案 »
- 急急!!! 数组赋值出错,请大家帮助看一下
- 问个非常奇怪的问题(C# WinForm VS2010)
- wcf 服务端 使用C++写的dll问题
- 有没有word方面的专家,我想用open方法打开word后,改变了内容后,按菜单中的退出按钮时,不提示保存直接退出
- 压缩tif文件
- 使用Reflector反编译组件后的代码中0x2faf080这些是什么意思?
- 百分求winform的完全可编辑的datagrid例子!
- 紧急提问
- c#中如何创建这样的网页?
- 如何在 Graphics 上将一幅彩色图像转换为灰度图像画出,其C#算法实现是怎样的?
- 急,怎么样让水晶报表页眉显示在报表页眉的上方呢?
- C#能读psd文件吗?把psd读成html
if(dr != null) Console.WriteLine(dr[""]);
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
string s =this.listView1.Columns[e.Column].Text;
}
<listview x:Name="myListName" ..../> <!--假设这里ItemsSource你已经绑定好了-->
<!-- 你提供的信息太少
假设这里 对应的属性名称是
ID,Name,Class,Date
123,张三,政治,2008-12-12
-->
<wrappanel>
<TextBox Text={Binding ElementName=myListName,Property=SelectedItem.ID}/>
<TextBox Text={Binding ElementName=myListName,Property=SelectedItem.Name}/>
<TextBox Text={Binding ElementName=myListName,Property=SelectedItem.Class}/>
<TextBox Text={Binding ElementName=myListName,Property=SelectedItem.Date}/>
</wrappanel>
不是用事件触发完成对数据的绑定。
是在 xaml 里设置 完成对数据的绑定。 就像对象的一对一的关系。它变化了,那么它对应的那项也跟着变化。
也可能默认就是TwoWay 我手写没测试
麻烦你测试下。100分送上
<ListView Height="186" Name="listView1" VerticalAlignment="Top" >
<ListView.View>
<GridView x:Name="TableFirst">
<GridViewColumn Header="借书证号" Width="98" DisplayMemberBinding="{Binding Path=借书证号}"></GridViewColumn>
<GridViewColumn Header="姓名" Width="98" DisplayMemberBinding="{Binding Path=姓名}"></GridViewColumn>
<GridViewColumn Header="图书名称" Width="98" DisplayMemberBinding="{Binding Path=图书名称}"></GridViewColumn>
<GridViewColumn Header="借书时间" Width="98" DisplayMemberBinding="{Binding Path=借书证号}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<!--设置list的datacontext 或者itemssource-->
<ListBox x:Name="list" ItemsSource="{Binding}" SelectedValuePath="Key">
<ListBox.ItemTemplate>
<DataTemplate>
<Wrappanel>
<TextBlock Text="{Binding Path=借书时间,StringFormat='yyyy-MM-dd'}"/>
<TextBlock Text="{Binding Path=借书证号}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!--属性是汉字?-->
<TextBox Text={Binding ElementName=list,Path=SelectedItem.借书时间}/>
<TextBox Text={Binding ElementName=list,Path=SelectedItem.借书证号}/>
你看下为什么还不行、<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="500" Width="400" Loaded="Window_Loaded">
<Grid>
<ListView Height="186" Name="listView1" VerticalAlignment="Top" >
<ListView.View>
<GridView x:Name="TableFirst">
<GridViewColumn Header="借书证号" Width="98" DisplayMemberBinding="{Binding Path=借书证号}"></GridViewColumn>
<GridViewColumn Header="姓名" Width="98" DisplayMemberBinding="{Binding Path=姓名}"></GridViewColumn>
<GridViewColumn Header="图书名称" Width="98" DisplayMemberBinding="{Binding Path=图书名称}"></GridViewColumn>
<GridViewColumn Header="借书时间" Width="98" DisplayMemberBinding="{Binding Path=借书证号}"></GridViewColumn>
</GridView>
</ListView.View>
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Path=借书证号}"/>
<TextBlock Text="{Binding Path=姓名}"/> <TextBlock Text="{Binding Path=图书名称}"/> <TextBlock Text="{Binding Path=借书时间}"/>
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Label HorizontalAlignment="Left" Margin="14,0,0,169" Name="label1" Width="95" Height="28" VerticalAlignment="Bottom">借书证号:</Label>
<Label Margin="0,0,87,167" Name="label2" Height="28" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="95">姓 名:</Label>
<Label Height="28" Margin="0,0,87,104" Name="label3" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="95">图书名称:</Label>
<Label Height="28" HorizontalAlignment="Left" Margin="14,0,0,104" Name="label4" VerticalAlignment="Bottom" Width="95">借书时间:</Label> <TextBox Text="{Binding ElementName=listview1,Path=SelectedItem.借书证号}" Margin="70,0,188,172" Name="textBox1" Height="23" VerticalAlignment="Bottom" />
<TextBox Text="{Binding ElementName=listview1,Path=SelectedItem.姓名}" Height="23" Margin="70,0,188,104" Name="textBox4" VerticalAlignment="Bottom" />
<TextBox Text="{Binding ElementName=listview1,Path=SelectedItem.图书名称}" Margin="0,0,6,104" Name="textBox2" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="120" />
<TextBox Text="{Binding ElementName=listview1,Path=SelectedItem.借书时间}" Margin="0,0,6,172" Name="textBox3" Height="23" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="120" />
</Grid>
</Window>
listbox比listview还轻量级
{
//在此处设断点,发现点击不同的Item后,此事件居然执行了2次 //第一次是取消当前Item选中状态,导致整个ListView的SelectedIndices变为0
//第二次才将新选中的Item设置为选中状态,SelectedIndices变为1
//如果不加listview.SelectedIndices.Count>0判断,将导致获取listview.Items[]索引超界的异常if (listview.SelectedIndices != null && listview.SelectedIndices.Count>0)
{
ListView.SelectedIndexCollection c = listview.SelectedIndices;
//在这把内容赋给textbox textbox1.text=listview.listview.Items[c[0]].subitem[0].text;}
}
while ((dep != null) && !(dep is ListViewItem))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null) return;
else
{
//要执行的任务 }ListView本身没有双击事件,只好用MouseDoubleClick来代替,问题就出现了,在双击ListView头的时候事件也会被触发.因此需要判断双击的项是否是ListViewItem.
if (实体 == null)
{
MessageBox.Show("请选择一条记录", "操作提示");
return;
}textbox1.text = 实体.属性
textbox2.text = 实体.属生1
这种方法非常简单.