2010-11-09 99 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