【问题标题】:Update binding on WPF Datagrid hover更新 WPF Datagrid 悬停上的绑定
【发布时间】:2021-03-22 19:21:02
【问题描述】:

我在绑定到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>

【问题讨论】:

  • setter 有不同的目的。你打算如何处理视图模型中的 IsHover 属性?
  • @ASh 我正在寻找处理一些逻辑并可能用它更新其他属性。问题是,我想分别跟踪 IsSelected 和 IsHover。 Setter 示例只是试图展示我希望或期望解决方案如何工作。

标签: c# wpf mvvm datagrid


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2011-02-25
    • 1970-01-01
    • 2011-06-27
    • 2021-08-31
    相关资源
    最近更新 更多