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

WPF-XAML-DataGridRow样式触发器IsMouseOver工具提示数据绑定,wpf,xaml,mvvm,Wpf,Xaml,Mvvm,我试图获得一个工具提示,在WPF中,鼠标悬停在DataGrid内的一行上时显示图像 以下是行样式的XAML的一个片段: <DataGrid.RowStyle> <Style TargetType="DataGridRow" > <Style.Triggers> <DataTrigger Binding="{Binding Path=CN

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

以下是行样式的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>
有人能帮忙吗?我再也不能松头发了

更新

感谢您的回复,Andy,以下是我的整个datagrid片段:

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

您不能编辑您发布的代码。 我不得不删除原来的帖子并创建另一个,只是为了将绑定更改为DynamicSource


我更新了OP以反映当前状态,但出于某些原因,它仍然不喜欢绑定:(除非您将该行放在xaml的其他位置,否则它会起作用。但现在不起作用。那么,当时和现在有什么不同呢?作为测试,我将页面资源放在xaml的顶部。然后我将工具提示放在了工具提示=“{DynamicResource yuserimg}”在我的另一个控件中,图像就在那里。这就像我的控件中的绑定出于某种原因不喜欢“userimg”。
<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>
            <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>
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')