本文介绍了检索单元格值上的MouseEnter在多个列的ListView数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从行中检索从车名列中的值在的ListView
在MouseEnter事件,并有结果显示在的TextBlock
。
我怎样才能做到这一点?
XAML
<网格和GT;
< ListView的X:名称=卡洛斯VerticalAlignment =顶部的MouseEnter =carList_MouseEnter>
< ListView.View>
<&GridView的GT;
< GridViewColumn标题=汽车DisplayMemberBinding ={绑定名称}WIDTH =自动/>
< GridViewColumn DisplayMemberBinding ={结合制作}标题=使WIDTH =自动/>
< GridViewColumn DisplayMemberBinding ={结合年度}标题=年WIDTH =自动/>
< / GridView的>
< /ListView.View>
< /&的ListView GT;
<文本框X:NAME =文本框HEIGHT =23保证金=5,0,0,5TextWrapping =包装VerticalAlignment =底/>
< /网格和GT;
CS
公共停车场SelectedCar {搞定;组; } 公共主窗口()
{
的InitializeComponent();
清单<汽车>汽车=新的List<汽车>(); INT I = 0;
字符串[] NAME = {的Sentra,IS,凯美瑞,S2000};
字符串[] =使{日产,凌志,丰田,本田};
字符串[] =一年{2000,2011,2013」,2004年}; 的foreach(在名称字符串s)
{
cars.Add(新车(){名称=名称[I],使=使[我],年=每年[I]});
我++;
} carList.ItemsSource =车; } 公共类车
{
公共字符串名称{;组; }
公共字符串制作{搞定;组; }
公共字符串年{搞定;组; } } 私人无效carList_MouseEnter(对象发件人,MouseEventArgs E)
{
变种carName =; //车的名称,显示悬停
textbox.Text = carName;
}
解决方案
试试这个:
XAML:
<网格和GT;
< ListView的X:名称=卡洛斯VerticalAlignment =顶部的MouseEnter =carList_MouseEnter>
< ListView.ItemContainerStyle>
<风格的TargetType ={X:输入一个ListBoxItem}>
< EventSetter事件=的MouseEnter处理器=ListBoxItem_MouseEnter/>
< /样式和GT;
< /ListView.ItemContainerStyle>
< ListView.View>
<&GridView的GT;
< GridViewColumn标题=汽车DisplayMemberBinding ={绑定名称}WIDTH =自动/>
< GridViewColumn DisplayMemberBinding ={结合制作}标题=使WIDTH =自动/>
< GridViewColumn DisplayMemberBinding ={结合年度}标题=年WIDTH =自动/>
< / GridView的>
< /ListView.View>
< /&的ListView GT;
<文本框X:NAME =文本框HEIGHT =23保证金=5,0,0,5TextWrapping =包装VerticalAlignment =底/>
< /网格和GT;
CS:
私人无效ListBoxItem_MouseEnter(对象S,MouseEventArgs E)
{
VAR项目= e.OriginalSource为ListBoxItem的;
VAR车= item.Content的车;
textbox.Text = car.Name;
}
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
.
How can I achieve this?
xaml
<Grid>
<ListView x:Name="carList" VerticalAlignment="Top" MouseEnter="carList_MouseEnter">
<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
public Car SelectedCar { get; set; }
public MainWindow()
{
InitializeComponent();
List<Car> cars = new List<Car>();
int i = 0;
string[] name = { "Sentra", "IS", "Camry", "s2000" };
string[] make = { "Nissan", "Lexus", "Toyota", "Honda" };
string[] year = { "2000", "2011", "2013", "2004" };
foreach (string s in name)
{
cars.Add(new Car() { Name = name[i], Make = make[i], Year = year[i] });
i++;
}
carList.ItemsSource = cars;
}
public class Car
{
public string Name { get; set; }
public string Make { get; set; }
public string Year { get; set; }
}
private void carList_MouseEnter(object sender, MouseEventArgs e)
{
var carName = ""; //Name of car to display on hover
textbox.Text = carName;
}
解决方案
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;
}
这篇关于检索单元格值上的MouseEnter在多个列的ListView数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!