簡體   English   中英

如何在C#背后的代碼中訪問datatemplate內部的網格控件名稱

[英]how to access the grid control name inside the datatemplate in code behind c#

如何訪問彈出窗口中的控件。 彈出窗口是可訪問的,但是為什么我不能訪問popup.data內部的數據網格,我們如何訪問DataTemplate內部的所有子控件?

 <DataGrid  ItemsSource="{Binding}" AutoGenerateColumns="False" Height="193" CanUserAddRows="False" 
 HorizontalAlignment="Left" Margin="48,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="560"
              CellEditEnding="dataGrid1_CellEditEnding" >
        <!--Resources Start-->
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridCell}">
                <EventSetter Event="GotFocus" Handler="DataGridCell_GotFocus" />
            </Style>
        </DataGrid.Resources>
        <!--Resources End-->
        <DataGrid.Columns>
            <!--Ledger Head Start-->
            <DataGridTemplateColumn Header="Ledger Head" Width="200" >

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding LedgerHead}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBox Text="{Binding LedgerHead}" Loaded="TextBox_Loaded"  KeyUp="TextBox_KeyUp" />
                            <Popup x:Name="popup1"  AllowsTransparency="True"  PopupAnimation="Slide" Width="500" Height="800"  PlacementTarget="{Binding ElementName=textPopUp}" >
                                <Grid>
                                    <DataGrid x:Name="datagrid2"></DataGrid>
                                </Grid>
                            </Popup>
                        </StackPanel>

                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

不,我們無法使用其名稱訪問數據模板內的控件。 但,

  1. 要將數據源分配給網格視圖,可以通過將sender強制轉換為gridview來在已加載的網格事件中進行處理。
  2. 要獲取網格的選定數據,可以通過將sender類型轉換為gridviewitem來使用grid的selectionchanged事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM