2013-12-08 58 views
0

我想從MouseEnter事件ListView行中的汽車名稱列中檢索值,並在TextBlock中顯示結果。檢索鼠標輸入在多列Co​​lumnView與數據綁定

我該如何做到這一點?

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; 
    } 

回答

2

試試這個:

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; 
} 
+0

This works great。再次感謝。 – MCSharp