WPF - XAML - DataGridRow 样式触发器 IsMouseOver 工具提示数据绑定(bind)

标签 wpf xaml mvvm

我试图获得一个工具提示,以在 WPF 中 DataGrid 内的一行鼠标悬停时显示图像。

以下是 RowStyle 的 XAML 片段:

            <DataGrid.RowStyle>
            <Style TargetType="DataGridRow" >
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=CNIsOnline,UpdateSourceTrigger=PropertyChanged}" Value="False">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.OfflineColor,UpdateSourceTrigger=PropertyChanged}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=CNIsOnline,UpdateSourceTrigger=PropertyChanged}" Value="True">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.OnlineColor,UpdateSourceTrigger=PropertyChanged}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=CNIsOnline,UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.UnknownColor,UpdateSourceTrigger=PropertyChanged}"></Setter>
                    </DataTrigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="DataContext" Value="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"/>
                        <Setter Property="ToolTip">
                            <Setter.Value>
                                <Image Height="50" Width="50" Source="{Binding userimg}"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>

我知道用户img 设置正确,因为如果我采取这一行:
<Image Height="50" Width="50" Source="{Binding userimg}"/>

并将其放在我的 XAML 中的其他位置,我会看到该图像。如果我将 userimg(它只是 PNG 路径的字符串)更改为硬编码路径,它似乎可以工作,所以我知道这是一个数据上下文问题。

我尝试了几件事,包括:this , this , this , 和 this但我仍在努力通过使用数据绑定(bind)来显示此图像。

我也试过这个,但也没有运气:
<Setter Property="ToolTip">
<Setter.Value>
    <Image Height="50" Width="50" Source={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.userimg,UpdateSourceTrigger=PropertyChanged}"/>
</Setter.Value>



这些是我尝试查看工具提示时出现的错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'userimg' property not found on 'object' ''DataRowView' (HashCode=6587426)'. BindingExpression:Path=userimg; DataItem='DataRowView' (HashCode=6587426); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=userimg; DataItem='DataRowView' (HashCode=6587426); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=userimg; DataItem='DataRowView' (HashCode=6587426); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=userimg; DataItem='DataRowView' (HashCode=6587426); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

System.Windows.Data Information: 41 : BindingExpression path error: 'userimg' property not found for 'object' because data item is null.  This could happen because the data provider has not produced any data yet. BindingExpression:Path=userimg; DataItem=null; target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=userimg; DataItem=null; target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=userimg; DataItem=null; target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=userimg; DataItem=null; target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

有人可以帮忙吗,不能再松开头发了...

更新

感谢安迪的回复,这是我的整个数据网格片段:
        <DataGrid Grid.Row="1" Grid.ColumnSpan="187"  Name="DG1" Background="Transparent" Foreground="{Binding MyForegroundColor}" BorderThickness="0" HeadersVisibility="Column" CanUserAddRows="False" IsReadOnly="True" Opacity="1" SelectionMode="Single" AutoGenerateColumns="True">
        <DataGrid.Resources>
            <Style TargetType="ScrollBar">
                <Setter Property="Opacity" Value=".3" />
            </Style>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding MyAccentColor,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{Binding MyForegroundColor,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"  Color="{Binding MyAccentColor,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="{Binding MyForegroundColor,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"/>
            <Image x:Key="yuserimg" Height="50" Width="50" Source="{Binding userimg}"/>
        </DataGrid.Resources>
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="HorizontalContentAlignment" Value="Left" />
                <Setter Property="FontWeight" Value="bold"/>
                <Setter Property="Margin" Value="0"/>
                <Setter Property="Padding" Value="1" />
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="BorderThickness" Value="1"/>
            </Style>
        </DataGrid.ColumnHeaderStyle>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow" >
                <Setter Property="ToolTip" Value="{Binding yuserimg}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=CNIsOnline,UpdateSourceTrigger=PropertyChanged}" Value="False">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.OfflineColor,UpdateSourceTrigger=PropertyChanged}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=CNIsOnline,UpdateSourceTrigger=PropertyChanged}" Value="True">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.OnlineColor,UpdateSourceTrigger=PropertyChanged}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=CNIsOnline,UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.UnknownColor,UpdateSourceTrigger=PropertyChanged}"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

但是,当我将鼠标悬停在一行上时,我现在根本看不到任何工具提示弹出。

更新 2

我更新了对动态资源的绑定(bind),如下所示:
            <DataGrid.Resources>
            <Style TargetType="ScrollBar">
                <Setter Property="Opacity" Value=".3" />
            </Style>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding MyAccentColor,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{Binding MyForegroundColor,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"  Color="{Binding MyAccentColor,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="{Binding MyForegroundColor,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"/>
            <Image x:Key="yuserimg" Height="50" Width="50" Source="{Binding userimg}"/>
        </DataGrid.Resources>


            <DataGrid.RowStyle>
            <Style TargetType="DataGridRow" >
                <Setter Property="ToolTip" Value="{DynamicResource yuserimg}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=CNIsOnline,UpdateSourceTrigger=PropertyChanged}" Value="False">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.OfflineColor,UpdateSourceTrigger=PropertyChanged}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=CNIsOnline,UpdateSourceTrigger=PropertyChanged}" Value="True">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.OnlineColor,UpdateSourceTrigger=PropertyChanged}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=CNIsOnline,UpdateSourceTrigger=PropertyChanged}" Value="{x:Null}">
                        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=DataContext.UnknownColor,UpdateSourceTrigger=PropertyChanged}"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>

但是,我仍然遇到绑定(bind)错误,我只看到一个 50x50 的框作为工具提示。

以下是绑定(bind)错误:
System.Windows.Data Information: 41 : BindingExpression path error: 'userimg' property not found for 'object' because data item is null.  This could happen because the data provider has not produced any data yet. BindingExpression:Path=userimg; DataItem=null; target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=userimg; DataItem=null; target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')
System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=userimg; DataItem=null; target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=userimg; DataItem=null; target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

最佳答案

您无法编辑发布的代码。
我不得不删除原始帖子并创建另一个只是为了更改绑定(bind)到动态资源。

关于WPF - XAML - DataGridRow 样式触发器 IsMouseOver 工具提示数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53675712/

相关文章:

c# - 在现代 UI (Metro) 图表中以编程方式 (MVVM) 添加 ChartSeries

xaml - Xamarin Forms 按钮内容布局不起作用

javascript - 在 WPF Web 浏览器中调用 Javascript,无需调度程序

ios - 如何在 MVVM 中存储 api 响应数据(非持久性)?

c# - 当 Windows XP 将屏幕淡化为灰色时如何通知我?

c# - 如何使 TreeView 和 ListBox 的 SelectedItem 保持同步?

c# - 如何从 ViewModel 访问 ObservableCollection 中的数据?

c# - 深色/浅色主题 Assets 限定符

c# - 如何在代码隐藏中将 ItemsPanelTemplate 设置为动态创建的网格

c# - 绑定(bind)到来自两个不同 ViewModel 的两个属性的差异