DataGrid - 鼠标输入+离开行事件

时间:2012-07-05 05:08:42

标签: wpf mouseevent wpfdatagrid

我想订阅鼠标进入和离开DataGridRow的事件。

我的DataGrid的XAML目前看起来像这样:

<toolkit:DataGrid ItemsSource="{Binding DisplayedSearchResults}" 
              caliburn:Message.Attach="[Event MouseDoubleClick] = [OpenDocument()]" 
              SelectedItem="{Binding SelectedRow, Mode=TwoWay}" Margin="7"
              AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"
              IsReadOnly="True" Grid.Row="0"
              Sorting="ResultsDataGrid_Sort">
    <toolkit:DataGrid.Resources>
        <Style TargetType="Button"></Style>
    </toolkit:DataGrid.Resources>
</toolkit:DataGrid>

如果可能的话,如何使用Caliburn进行此操作,如果没有,可以使用代码隐藏?

1 个答案:

答案 0 :(得分:1)

我结束了代码隐藏:

MyView.xaml.cs:

public partial class MyView : UserControl
{

    public SearchResultsView()
    {
        InitializeComponent();
        SearchResultsGrid.LoadingRow += DataGrid_PreparingRow;
    }        

    public void DataGrid_PreparingRow(object sender, DataGridRowEventArgs args)
    {
        args.Row.MouseEnter += Row_MouseEnter;
        args.Row.MouseLeave += Row_MouseLeave;
    }

    public void Row_MouseEnter(object sender, MouseEventArgs args)
    {
        // do some stuff
    }

    public void Row_MouseLeave(object sender, MouseEventArgs args)
    {
        // do some stuff
    }
}

MyView.xaml:

<UserControl x:Class="MyView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             
             xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <toolkit:DataGrid Name="SearchResultsGrid" />
    </Grid>
</UserControl>