[英]How to make a cell in a DATAGRID link to another Window?
I get data from an SQL database into my DATAGRID on my WPF application. 我将数据从SQL数据库获取到WPF应用程序中的DATAGRID中。 I want to be able to click a cell that is named : 'Left to audit' and from there get redirected to anothter page with all the information on how many is left to audit.
我希望能够单击一个名为“ Left to audit”的单元格,然后从该单元格重定向到另一个页面,其中包含有关剩下多少要审核的所有信息。
How do i go about creating the click event to take me to another page? 如何创建点击事件以将我带到另一个页面?
PS I am a Novice. PS我是新手。
Edit: http://i.stack.imgur.com/LGnHA.png 编辑: http : //i.stack.imgur.com/LGnHA.png
Edit: http://i.stack.imgur.com/tU0bA.png - Want to click in the cells on the last column. 编辑: http : //i.stack.imgur.com/tU0bA.png-想单击最后一列的单元格。
Hope this work: 希望这项工作:
<DataGridHyperlinkColumn Binding="{Binding Link}">
<DataGridHyperlinkColumn.ElementStyle>
<Style>
<EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
private void DG_Hyperlink_Click(object sender, RoutedEventArgs e)
{
Hyperlink link = (Hyperlink)e.OriginalSource;
Process.Start(link.NavigateUri.AbsoluteUri);
}
If the URI points a website it will be opened with the default web-browser, if it is a folder it will be opened in explorer, if it is a file it will be opened with the default application associated with it. 如果URI指向一个网站,它将使用默认的Web浏览器打开;如果它是一个文件夹,它将在资源管理器中打开;如果它是一个文件,则将使用与其关联的默认应用程序打开。
To use this for autogenerated columns your property needs to be of type Uri so a DataGridHyperlinkColumn is generated. 要将其用于自动生成的列,您的属性必须为Uri类型,以便生成DataGridHyperlinkColumn。 You then can hook up the event by placing the style in the DataGrid.Resources:
然后,可以通过将样式放在DataGrid.Resources中来挂接事件:
<DataGrid.Resources>
<Style TargetType="Hyperlink">
<EventSetter Event="Click" Handler="DG_Hyperlink_Click"/>
</Style>
</DataGrid.Resources>
Try This.. 尝试这个..
Add an EventSetter on the CellStyle: 在CellStyle上添加一个EventSetter:
<DataGrid.CellStyle>
<Style>
<EventSetter Event="DataGridCell.MouseLeftButtonDown"
Handler="CellClicked" />
</Style>
</DataGrid.CellStyle>
In Code Behind add Handler: 在后面的代码中添加处理程序:
private void CellClicked(object sender, MouseButtonEventArgs e)
{
String cellContent = ((TextBlock)sender).Text;
xamlAllocateAudit window = new xamlAllocateAudit
{
DataContext = cellContent
}
window.Show();
}
Works on my end.. First click selects cell, second click fires handler, which opens the new window. 在我的一端工作。第一次单击选择单元格,第二次单击触发处理程序,这将打开新窗口。
if you want the same window to be updated, then keep a reference of the window, if existing, update it's datacontext. 如果要更新同一窗口,则保留该窗口的引用(如果存在),则更新它的datacontext。
On the ohter side in the xamlAllocateAudit, create handler for the event "DataContextChanged": 在xamlAllocateAudit的另一端,创建事件“ DataContextChanged”的处理程序:
<Window x:Class="WpfApplication3.xamlAllocateAudit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DetailsWindow" Height="300" Width="300"
DataContextChanged="Window_DataContextChanged">
<!-- Some graphics -->
</Window>
And in CodeBehind: 在CodeBehind中:
private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var newDataContext = e.NewValue;
//do stuff with DataContext
}
Cheers!! 干杯!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.