更新 WPF Datagrid 悬停时的绑定

Update binding on WPF Datagrid hover

我在绑定到 List<Object> 的 MVVM 设计中有一个简单的 WPF 数据网格。我的目标是在用户将鼠标悬停在该行上时更新其中一个属性。我一直在研究样式触发器,interaction.trigger,但似乎找不到有用的东西。感谢您的帮助!

型号:

public class CarrierInvDetails : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged
    private bool _IsHover;
    public bool IsHover
    {
        get
        {
            return _IsHover;
        }
        set
        {
            PropertyChanged.ChangeAndNotify(ref _IsHover, value, () => IsHover);
        }
    }
}

视图模型:

private List<CarrierInvDetails> _CarrierInvList;
public List<CarrierInvDetails> CarrierInvList
{
    get
    {
        return _CarrierInvList;
    }
    set
    {
        PropertyChanged.ChangeAndNotify(ref _CarrierInvList, value, () => CarrierInvList);
    }
}

观点:

<DataGrid ItemsSource="{Binding CarrierInvList}" 
            Margin="5"
            SelectedItem="{Binding SelectedCarrierInv}"
            CanUserAddRows="False"
            CanUserDeleteRows="False"
            IsReadOnly="True">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow" BasedOn="{StaticResource MahApps.Styles.DataGridRow}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <!--This is kind of what Id like to do.  When the mouse is over the row, update IsHover to True, but it complains about having a "Binding" here -->
                    <Setter Property="{Binding IsHover}" Value="True"/> 
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

对于未来的任何人,我都能够通过结合使用事件、界面和样式触发器来获得可行的解决方案。

首先,设置数据网格样式:

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow" BasedOn="{StaticResource MahApps.Styles.DataGridRow}">
        <!-- This is where we will set the IsHover property -->
        <EventSetter Event="MouseEnter" Handler="DataGridRow_Enter" /> 
        <EventSetter Event="MouseLeave" Handler="DataGridRow_Leave" />
        <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsHover}" Value="True">
                <Setter Property="Background" Value="DarkSeaGreen"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

界面:

public interface IHover
{
    public bool IsHover
    {
        get;
        set;
    }
}

然后是实际设置值的代码隐藏

private void DataGridRow_Enter(object sender, MouseEventArgs e)
{
    if (sender.GetType() != typeof(DataGridRow))
        return;

    if (typeof(Models.IHover).IsAssignableFrom((sender as DataGridRow).DataContext.GetType()))
    {
        ((Models.IHover)((sender as DataGridRow).DataContext)).IsHover = true;
    }
}

private void DataGridRow_Leave(object sender, MouseEventArgs e)
{
    if (sender.GetType() != typeof(DataGridRow))
        return;

    if (typeof(Models.IHover).IsAssignableFrom((sender as DataGridRow).DataContext.GetType()))
    {
        ((Models.IHover)((sender as DataGridRow).DataContext)).IsHover = false;
    }
}

最后是利用接口的模型

public class ClientInvDetails : INotifyPropertyChanged, IHover
{
    public event PropertyChangedEventHandler PropertyChanged;
    private bool _IsHover;
    public bool IsHover
    {
        get
        {
            return _IsHover;
        }
        set
        {
            //This is simply an extension that handles the notification of a property change.  You can use a standard "OnPropertyChanged" function that is available elsewhere
            PropertyChanged.ChangeAndNotify(ref _IsHover, value, () => IsHover);
        }
    }

    private bool _IsSelected;
    public bool IsSelected
    {
        get
        {
            return _IsSelected;
        }
        set
        {
            PropertyChanged.ChangeAndNotify(ref _IsSelected, value, () => IsSelected);
        }
    }
}