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

时间:2018-12-07 19:26:45

标签: 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>

我知道 userimg 设置正确,因为如果我走这行:

<Image Height="50" Width="50" Source="{Binding userimg}"/>

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

我已经尝试了多种方法,包括:thisthisthisthis,但我仍在努力通过使用数据绑定来显示此图像。

我也没有运气尝试过这个

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

有人可以帮忙吗,不能再失去头发了...

更新

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

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

更新2

我这样更新了对dynamicresource的绑定:

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

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

以下是绑定错误:

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

1 个答案:

答案 0 :(得分:0)

您无法编辑您发布的代码。 我不得不删除原始帖子并创建另一个帖子,只是为了更改对dynamicresouree的绑定。