2010-11-09 102 views
0

我有一个数据表绑定到WPF中的数据网格。现在点击网格中的一行,我需要弹出一个窗口。但为此,我需要首先将数据网格中的列更改为超链接。任何想法如何做到这一点?WPF Datagrid自动生成列

<DataGrid Name="dgStep3Details" Grid.Column="1" Margin="8,39,7,8" IsReadOnly="True" ItemsSource="{Binding Mode=OneWay, ElementName=step3Window,Path=dsDetails}" /> 

如果我不能将自动生成的列更改为超链接,是否有方法将按钮添加到每一行?

感谢 尼基尔

回答

1

所以,很难创建超链接列来自动生成数据网格。我最终做的是这样的 - 根据数据网格的自动生成事件将我的代码放在哪里,然后为网格创建按钮并为其添加路由事件。我不希望我的代码被硬编码到列上,现在我可以通过即时更改数据表来灵活操作。这里是代码:

private void dgStep3Details_AutoGeneratedColumns(object sender, EventArgs e) 
    { 

     DataGrid grid = sender as DataGrid; 
     if (grid == null) 
      return; 
     DataGridTemplateColumn col = new DataGridTemplateColumn(); 
     col.Header = "More Details"; 
     FrameworkElementFactory myButton = new FrameworkElementFactory(typeof(Button), "btnMoreDetails"); 
     myButton.SetValue(Button.ContentProperty, "Details"); 
     myButton.AddHandler(Button.ClickEvent, new RoutedEventHandler(btnMoreDetails_Click)); 
     DataTemplate cellTempl = new DataTemplate(); 
     //myButton.SetValue(Button.CommandParameterProperty, ((System.Data.DataRowView)((dgStep3Details.Items).CurrentItem)).Row.ItemArray[0]); 
     cellTempl.VisualTree = myButton; 
     col.CellTemplate = cellTempl; 
     dgStep3Details.Columns.Add(col); 

    } 
    public void btnMoreDetails_Click(object sender, RoutedEventArgs e) 
    { 
     //Button scrButton = e.Source as Button; 
     string currentDetailsKey = ((System.Data.DataRowView)(dgStep3Details.Items[dgStep3Details.SelectedIndex])).Row.ItemArray[0].ToString(); 
     // Pass the details key to the new window 

    } 
0

我不认为你将能够获得这些高级功能的用户界面自动生成出列。我想你要么决定在C#或VB.NET中编写这些列,当你检索你的数据并按你喜欢的方式定制它们,或者你必须放弃你提到的UI想法。自动生成的列不能做到这一点。

但是,你可以改变你的方法。尝试检查MouseLeftButtonDown等事件,并查看是否可以通过其他方式模拟您想要的行为。

+0

里,谢谢你的回答。我同意,我不得不不带自动生成功能,然后尝试ItemDataTemplates将我的列设置为超链接。我也想过使用这种方法http://msdn.microsoft.com/en-us/library/aa984262(​​v=VS.71).aspx,但这也不使用自动生成的列,因此使用下面的解决方案 – Nikhil 2010-11-10 03:36:40