wpf,listbox请教。 我在每个listboxItem里都加了一个按钮,点击按钮后,如何知道点击的按钮是属于哪个item呢?谢谢~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 高手么,可否说的详情点儿?item.owner里的item是指listbox的item吗?parent我试过了,不行。绑定datacontext 或者tag 具体要怎么做呢? 写个datatemplate,然后再写个model存放你的数据,例如显示的东西,button上面的文字等,一个model对应的就是一个item,点击按钮时,按钮的datacontext就是那个model using System.Collections.ObjectModel;using System.Windows;namespace TestMultiLines{ /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 { public Window1() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { ObservableCollection<TestModel> test = new ObservableCollection<TestModel>(); test.Add(new TestModel {test = "1"}); test.Add(new TestModel {test = "2"}); test.Add(new TestModel {test = "3"}); test.Add(new TestModel {test = "4"}); test.Add(new TestModel {test = "5"}); listView.DataContext = test; } private void Button_Click(object sender, RoutedEventArgs e) { MessageBox.Show(sender.ToString()); } } class TestModel : DependencyObject { public string test { get { return (string)GetValue(testProperty); } set { SetValue(testProperty, value); } } public static DependencyProperty testProperty = DependencyProperty.Register("test", typeof(string), typeof(TestModel)); }} <Window x:Class="TestMultiLines.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Loaded="Window_Loaded" Title="Window1" Height="300" Width="300"> <Window.Resources> <DataTemplate x:Key="testTemplate"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=test}" /> <Button Content="{Binding Path=test}" Click="Button_Click" /> </StackPanel> </DataTemplate> </Window.Resources> <Grid> <ListView Name="listView" ItemTemplate="{StaticResource testTemplate}" ItemsSource="{Binding}" /> </Grid></Window> 楼上的方法只适用与很简单的数据结构。点击按钮后,sender的类型也只是一个Button,而不是TestModel,我想得到按钮对应的TestModel 。 sender是个button,你看看(sender as Button).DataContext是什么,这个方法应该适用于任何数据结构,界面的呈现Model可以定义的很复杂的 关键点是这句:(sender as Button).DataContext明白了,非常感谢!! 版主求教 急······· C#中将byte[]数组转换为24bit bmp图像 急... DataSet中添加多个记录问题 求c#程序打包瘦身方案。 输入法问题,求助 “Form1”: 成员名称不能与它们的封闭类型相同 关于权限设置的问题 将软件狗的命名空间using进来怎么做 关于读取交换机,网卡等的状态的问题? 如何在WEBFROM中通过按钮实现关闭和最小化?谢谢~ C# 一些默认按键的问题 求一个简单实用的uml 工具的下载资源,如(Visual Paradigm...等)
item.owner里的item是指listbox的item吗?
parent我试过了,不行。
绑定datacontext 或者tag 具体要怎么做呢?
using System.Windows;namespace TestMultiLines
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1
{
public Window1()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<TestModel> test = new ObservableCollection<TestModel>();
test.Add(new TestModel {test = "1"});
test.Add(new TestModel {test = "2"});
test.Add(new TestModel {test = "3"});
test.Add(new TestModel {test = "4"});
test.Add(new TestModel {test = "5"});
listView.DataContext = test;
} private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(sender.ToString());
}
} class TestModel : DependencyObject
{
public string test
{
get { return (string)GetValue(testProperty); }
set { SetValue(testProperty, value); }
} public static DependencyProperty testProperty = DependencyProperty.Register("test", typeof(string),
typeof(TestModel));
}
}
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Window_Loaded"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="testTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=test}" />
<Button Content="{Binding Path=test}" Click="Button_Click" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListView Name="listView" ItemTemplate="{StaticResource testTemplate}" ItemsSource="{Binding}" />
</Grid>
</Window>
点击按钮后,sender的类型也只是一个Button,而不是TestModel,
我想得到按钮对应的TestModel 。
(sender as Button).DataContext明白了,非常感谢!!