wpf - 如何获取网格行的数据上下文

标签 wpf data-binding datacontext

我有一个数据网格,其中的行绑定(bind)到 View 模型。

在我的第一列中,我有一个图像控件,它绑定(bind)到该行中的 itemno。这工作得很好。

现在,我想要在该图像上显示一个工具提示,以显示该图像的放大版本。我认为这很容易,但我意识到,工具提示可能无法获取行的数据上下文。

那么,如何获取工具提示,以绑定(bind)到与行绑定(bind)到的相同数据? 在下面的代码中,工具提示上的 ItemNo 为 null,但在“父”图像控件上,它具有正确的值。

<DataGridTemplateColumn Header="Image" IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <controls:ItemImage Width="16" Height="16" DataContext="{Binding ItemNo, IsAsync=True}" >
                                        <controls:ItemImage.ToolTip>
                                            <StackPanel>
                                                <controls:ItemImage Width="300" Height="300" DataContext="{Binding ItemNo, IsAsync=True}" />
                                                <Label Content="{Binding ItemNo}" />
                                            </StackPanel>
                                        </controls:ItemImage.ToolTip>
                                    </controls:ItemImage>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

最佳答案

默认情况下,您的工具提示 dataContext 已设置为 ItemNo;这就是为什么将其重置为:

<controls:ItemImage Width="300" Height="300" DataContext="{Binding ItemNo, IsAsync=True}" />
...不起作用。

只需删除 DataContext="{Binding ItemNo, IsAsync=True}"

使用snoop它将帮助您解决 viewwing 绑定(bind)错误或已设置的 dataContext。

关于wpf - 如何获取网格行的数据上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15243132/

相关文章:

c# - C# 中的数据绑定(bind) - 字符串形式的属性名称

c# - 您如何在 Web 应用程序中集中 Entity Framework 数据上下文?

c# - 在 Entity Framework 代码优先方法中创建的数据库的默认物理位置

c# - 自定义 ComboBox 样式看不到所选项目文本

javascript - asp.net ajax 4.0客户端模板,如何绑定(bind)一个select?

c# - 使用 WPF 将应用程序最小化/关闭到系统托盘

wpf - 在双向绑定(bind)中将 IValueConverter 与当前 DataContext 一起使用

c# - 为什么我的交易没有升级为 DTC?

wpf - NAudio修剪mp3文件

c# - 按下按钮上的选项卡时更改按钮上的焦点颜色