[英]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.