[英]Not able to get the cell details in WPF DataGrid cell
我有一个数据网格,其中项目源与数据库中的数据绑定在一起。它有两个数据模板,使用值转换器来限制值(我已经使用转换器将员工ID(frow1列)转换为图像路径)。现在我想显示员工ID当我成功运行了填充有员工图像的应用程序数据网格时,用户双击带有图像的单元格。
到目前为止,我已经尝试使用下面的代码中所示的DataGridCellInfo进行如下操作。我在数据网格xamal中设置了CurrentCell =“ {Binding CellInfo,Mode = TwoWay}”。这里CellInfo是公共属性
<DataGrid x:Name="dtGrid" AutoGenerateColumns="False"
Margin="0,0,0,0" SelectionUnit="Cell"
HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"
SelectionMode="Single"
CurrentCell="{Binding CellInfo, Mode=OneWayToSource}"
VerticalAlignment="Top" RowHeight="50" ColumnWidth="50"
AlternatingRowBackground="{x:Null}" AlternationCount="2"
CanUserResizeRows="False" CanUserAddRows="False" CanUserDeleteRows="False"
CanUserReorderColumns="False" CanUserResizeColumns="False"
CanUserSortColumns="False" HeadersVisibility="None"
GridLinesVisibility="None" HorizontalGridLinesBrush="{x:Null}"
VerticalGridLinesBrush="{x:Null}" >
<DataGrid.Columns>
<DataGridTemplateColumn Width="SizeToCells" IsReadOnly="True" > <DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Button Background="#FF1D5BBA"
PreviewMouseDoubleClick="Button_PreviewMouseDoubleClick" >
<Image Source="{Binding Path=frow1,
Converter=StaticResource
prfileconverter},
Mode=Default}" />
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我后面的代码:
private void Button_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
MessageBox.Show(CellInfo.ToString())
}
private DataGridCellInfo _cellInfo;
public DataGridCellInfo CellInfo
{
get {
return _cellInfo;
}
set
{
_cellInfo = value;
OnPropertyChanged("CellInfo");
//this is to refresh through INotifyPropertyChanged
}
}
在这里,我被困在如何在单元格上执行MouseDoubleClick时如何显示员工ID。当我双击该单元格时,我得到的消息框显示为“ System.Windows.Control.DataGridCellInfo”。我没有得到单元格项目(员工ID)
问题是DataGridTemplateColumn
无法访问CellInfo.Item
(它不存在)
最简单的方法是通过Button_PreviewMouseDoubleClick
事件,将Button的Tag属性绑定到您的数据,然后在事件处理程序中访问它,如下所示:
<Button PreviewMouseDoubleClick="Button_PreviewMouseDoubleClick" Tag="{Binding Path=frow1}">....
然后在事件处理程序中执行以下操作:
private void Button_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var button = (Button)sender;
MessageBox.Show(button.Tag.ToString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.