2013-04-15 37 views
-2

我在使用xaml設計的datagrid中有一個文本框。我可以使用datagrid的事件訪問之前在代碼文件中使用xaml設計的文本框。請幫我.....................WPF-使用特定的datagrid事件訪問存在於datagrid(xaml)中的文本框

<Window x:Class="GridTextBox.Test" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowState="Maximized" 
    Title="Test" Height="300" Width="300" Loaded="Window_Loaded"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="30"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="30"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width=".25*"/> 

     <ColumnDefinition Width=".25*"/> 
     <ColumnDefinition Width=".25*"/> 
     <ColumnDefinition Width=".25*"/> 
    </Grid.ColumnDefinitions> 
    <DataGrid Grid.Row="1" Grid.Column="1" Name="datagrid1" SelectionChanged="datagrid1_SelectionChanged" LoadingRowDetails="DataGrid_LoadingRowDetails" Height="auto" Width="auto"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBox Name="txtEmpid" Text="hiiiiii"></TextBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

+0

我可以訪問使用任何DataGrid的事件在數據網格中的文本? - @ BasiconWPF –

回答

0

你可以使用DataGrid的下列擴展。
只需創建一個新模塊並插入以下代碼即可。

Module Extensions 
<Extension()> 
    Public Function GetDataTemplateObjectFromSelectedRow(Of DataTemplateType As Visual)(ByVal lDataGrid As DataGrid, ByVal lColumnIndex As Integer) As DataTemplateType 
    Dim lRow As DataGridRow 
    Dim lCell As DataGridCell 
    Dim lPresenter As DataGridCellsPresenter 
    Dim lFoundObject As DataTemplateType = Nothing 

    If lDataGrid.SelectedIndex >= 0 Then 
     lRow = lDataGrid.ItemContainerGenerator.ContainerFromIndex(lDataGrid.SelectedIndex) 
     lPresenter = GetVisualChild(Of DataGridCellsPresenter)(lRow) 
     lCell = lPresenter.ItemContainerGenerator.ContainerFromIndex(lColumnIndex) 
     lFoundObject = GetVisualChild(Of DataTemplateType)(lCell) 
    End If 
    Return lFoundObject 
    End Function 

    Public Function GetVisualChild(Of T As Visual)(parent As Visual) As T 
    Dim child As T = Nothing 
    Dim numVisuals As Integer = VisualTreeHelper.GetChildrenCount(parent) 
    For i As Integer = 0 To numVisuals - 1 
     Dim v As Visual = DirectCast(VisualTreeHelper.GetChild(parent, i), Visual) 
     child = TryCast(v, T) 
     If child Is Nothing Then 
     child = GetVisualChild(Of T)(v) 
     End If 
     If child IsNot Nothing Then 
     Exit For 
     End If 
    Next 
    Return child 
    End Function 

End Module 



之後,你可以調用下面的函數在數據網格的事件:

Private Sub DataGridButtons_CurrentCellChanged(sender As Object, e As EventArgs) Handles DataGridButtons.CurrentCellChanged 
    Dim lTextBox As TextBox = DataGridButtons.GetDataTemplateObjectFromSelectedRow(Of TextBox)(0) 
    End Sub 

的部分(Of TextBox)描述您的預計數據類型與參數(0)您所描述的相關GridColumn

+0

任何人都可以回答,爲什麼上面發佈的代碼不能正確格式化?如果我在另一個帖子編輯窗口中輸入我的答案的內容,它會正確顯示。 – Basti

+0

我可以在datagrid事件中使用它...即在xaml中設計的datagrid中...用於代碼文件中特定的datagrid事件? –

+0

我編輯了我的帖子。您可以在代碼文件中的事件處理程序中調用該函數。我使用了CurrentCellChanged事件,你使用哪個? – Basti

0

解決方案C#

namespace ExtensionMethods 
{ 
    public static class Extensions 
    { 

    public static DataTemplateType GetDataTemplateObjectFromSelectedRow<DataTemplateType>(DataGrid lDataGrid, int lColumnIndex) where DataTemplateType : Visual 
    { 
     DataGridRow lRow = default(DataGridRow); 
     DataGridCell lCell = default(DataGridCell); 
     DataGridCellsPresenter lPresenter = default(DataGridCellsPresenter); 
     DataTemplateType lFoundObject = null; 

     if (lDataGrid.SelectedIndex >= 0) 
     { 

     lRow = (DataGridRow)lDataGrid.ItemContainerGenerator.ContainerFromIndex(lDataGrid.SelectedIndex); 
     lPresenter = GetVisualChild<DataGridCellsPresenter>(lRow); 
     lCell = (DataGridCell)lPresenter.ItemContainerGenerator.ContainerFromIndex(lColumnIndex); 
     lFoundObject = GetVisualChild<DataTemplateType>(lCell); 
     } 
     return lFoundObject; 
    } 

    public static T GetVisualChild<T>(Visual parent) where T : Visual 
    { 
     T child = null; 
     int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
     for (int i = 0; i <= numVisuals - 1; i++) 
     { 
     Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); 
     child = v as T; 
     if (child == null) 
     { 
      child = GetVisualChild<T>(v); 
     } 
     if (child != null) 
     { 
      break; // TODO: might not be correct. Was : Exit For 
     } 
     } 
     return child; 
    } 

    } 
} 

使用以下調用來獲得文本框:

private void DataGridButtons_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    TextBox lTextBox = Extensions.GetDataTemplateObjectFromSelectedRow<TextBox>(DataGridButtons, 0); 
} 
+0

我可以通過使用這個獲得文本框的ID嗎?即我可以在網格事件中使用文本框ID ....假設我的文本框名稱是「txtClient」,我可以使用它用於任何目的,如調用方法..例如:在datagrid事件中的txtClient.CallService() –

+0

什麼發佈代碼的問題?只需使用上面第二個代碼塊中的示例即可。之後,你可以做你的「lTextBox.CallService」。對象「lTextBox」是你想要的。 – Basti

+0

可以幫我解決上面的問題嗎? –