gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:19:07 25 4
gpt4 key购买 nike

我试图获得一个工具提示,以在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com