简体   繁体   English

更新 WPF Datagrid hover 上的绑定

[英]Update binding on WPF Datagrid hover

I have a simple WPF datagrid in an MVVM design that is bound to a List<Object> .我在绑定到List<Object>的 MVVM 设计中有一个简单的 WPF 数据网格。 My goal is to update one of the properties when the user hovers over that row.我的目标是在用户将鼠标悬停在该行上时更新其中一个属性。 I've been investigating style triggers, interaction.trigger, and just can't seem to find something that works.我一直在研究风格触发器,interaction.trigger,但似乎找不到有用的东西。 Thanks for the help!谢谢您的帮助!

The model: model:

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

The Viewmodel:视图模型:

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

The View:风景:

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

For anyone in the future, I was able to get a working solution by using a combination of Events, interface, and Style triggers.对于未来的任何人,我都能通过使用事件、界面和样式触发器的组合来获得可行的解决方案。

First, setting the datagrid style:一、设置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>

The interface:界面:

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

Then the code-behind that actually sets the values然后是实际设置值的代码隐藏

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

And finally the model that utilizes the interface最后是使用该接口的 model

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM