自定义列,后台如何获取自定义列当中的控件

被遺忘的榕樹 2023-08-22 17:41:34

为满足自定义表头样式,我将grid修改为

 

 <DataGrid  Name="dgv" Grid.Row="0" ItemsSource="{Binding}" BeginningEdit="dgv_BeginningEdit"  Panel.ZIndex="3" AutoGenerateColumns="False" CanUserAddRows="False" LoadingRow="dgv_LoadingRow"  SelectedCellsChanged="dgv_SelectedCellsChanged" Grid.ColumnSpan="2" Margin="2,0,0,7">
                <DataGrid.Columns>
                   
                    <DataGridTemplateColumn x:Name="baseInfoColumn" Width="5*">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                <Setter Property="Height" Value="60"/>
                                <Setter Property="VerticalAlignment" Value="Top"/>
                                <Setter Property="Height" Value="100"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="DataGridColumnHeader">
                                            <Grid Height="100">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                   
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="30"/>
                                                    <RowDefinition Height="100"/>
                                                </Grid.RowDefinitions>
                                                <Border  Grid.Column="0" Grid.ColumnSpan="5" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 0">
                                                    <TextBlock
                                                       HorizontalAlignment="Center" VerticalAlignment="Center"
                                                       Text="NiOx-1"/>
                                                </Border>
                                                <Border Grid.Column="0" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="Ar&#x000A;sccm&#x000A;(0-10000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="1" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="主-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="2" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="T1-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="3" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="T2-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="4" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="T3-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                            </Grid>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Grid >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <!--<Border BorderBrush="Black" BorderThickness="0,0,1,0">-->
                                    <TextBox x:Name="NiOx1MAr" Grid.Column="0"  Text="{Binding NiOx1MAr, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"   Margin="0"/>
                                    <!--</Border>-->
                                    <TextBox x:Name="NiOx1MO2" Grid.Column="1" Text="{Binding NiOx1MO2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"  Margin="0"/>
                                    <TextBox x:Name="NiOx1T1O2" Grid.Column="2" Text="{Binding NiOx1T1O2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"  Margin="0"/>
                                    <TextBox x:Name="NiOx1T2O2" Grid.Column="3" Text="{Binding NiOx1T2O2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"   Margin="0"/>
                                    <TextBox x:Name="NiOx1T3O2" Grid.Column="4" BorderThickness="0"  Text="{Binding NiOx1T3O2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"  Margin="0"/>
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn x:Name="baseInfoColumn2" Width="5*">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                <!--<Setter Property="Height" Value="60"/>-->
                                <Setter Property="VerticalAlignment" Value="Top"/>
                                <Setter Property="Height" Value="100"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="DataGridColumnHeader">
                                            <Grid Height="100">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                   
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="30"/>
                                                    <RowDefinition Height="100"/>
                                                </Grid.RowDefinitions>
                                                <Border  Grid.Column="0" Grid.ColumnSpan="5" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 0">
                                                    <TextBlock
                                                       HorizontalAlignment="Center" VerticalAlignment="Center"
                                                       Text="NiOx-2"/>
                                                </Border>
                                                <Border Grid.Column="0" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="Ar&#x000A;sccm&#x000A;(0-10000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="1" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="主-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="2" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="T1-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="3" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="T2-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="4" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="T3-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>

                                            </Grid>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Grid >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <TextBox x:Name="NiOx2MAr" Grid.Column="0" Text="{Binding NiOx2MAr, UpdateSourceTrigger=PropertyChanged}"  Style="{DynamicResource GridTexboxStyle}" Margin="0"/>
                                    <TextBox x:Name="NiOx2MO2" Grid.Column="1" Text="{Binding NiOx2MO2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"  Margin="0"/>
                                    <TextBox x:Name="NiOx2T1O2" Grid.Column="2" Text="{Binding NiOx2T1O2, UpdateSourceTrigger=PropertyChanged}"  Style="{DynamicResource GridTexboxStyle}" Margin="0"/>
                                    <TextBox x:Name="NiOx2T2O2" Grid.Column="3" Text="{Binding NiOx2T2O2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}" Margin="0"/>
                                    <TextBox x:Name="NiOx2T3O2" Grid.Column="4"  BorderThickness="0"  Text="{Binding NiOx2T3O2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}" Margin="0"/>
                                </Grid>
                               
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn  Header="RF1&#x000A;KW&#x000A;(0-6KW)"  Width="*"  >
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style  TargetType="DataGridColumnHeader">
                                <Setter Property="Height" Value="100"/>
                                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                <Setter Property="VerticalAlignment" Value="Top"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <TextBox x:Name="txtboxrf1" Text="{Binding RFCurrent1, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Height="100"/>

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn  Header="RF2&#x000A;KW&#x000A;(0-6KW)" Width="*">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style  TargetType="DataGridColumnHeader">
                                <Setter Property="Height" Value="100"/>
                                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                <Setter Property="VerticalAlignment" Value="Top"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox x:Name="txtboxrf2" Text="{Binding RFCurrent2, UpdateSourceTrigger=PropertyChanged}"  BorderThickness="0"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>


                    <DataGridTemplateColumn x:Name="baseInfoColumnito" Width="5*">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="DataGridColumnHeader">
                                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                <Setter Property="Height" Value="60"/>
                                <Setter Property="VerticalAlignment" Value="Top"/>
                                <Setter Property="Height" Value="100"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="DataGridColumnHeader">
                                            <Grid Height="100" Margin="0">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                    
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="30"/>
                                                    <RowDefinition Height="100"/>
                                                </Grid.RowDefinitions>
                                                <Border  Grid.Column="0" Grid.ColumnSpan="5" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 0">
                                                    <TextBlock
                                                       HorizontalAlignment="Center" VerticalAlignment="Center"
                                                       Text="ITO"/>
                                                </Border>
                                                <Border Grid.Column="0" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="Ar&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="1" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="主-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="2" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="T1-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="3" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="T2-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                                <Border Grid.Column="4" Grid.Row="1" BorderBrush="AntiqueWhite" BorderThickness="0 1 1 1" Margin="0,0,0,30">
                                                    <TextBlock Text="T3-O2&#x000A;sccm&#x000A;(0-1000)" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </Border>
                                            </Grid>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>

                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <TextBox x:Name="ITOMAr" Grid.Column="0" Text="{Binding ITOMAr, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"  Margin="0"/>
                                    <TextBox x:Name="ITOMO2" Grid.Column="1" Text="{Binding ITOMO2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"  Margin="0"/>
                                    <TextBox x:Name="ITOT1O2" Grid.Column="2" Text="{Binding ITOT1O2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"  Margin="0"/>
                                    <TextBox x:Name="ITOT2O2" Grid.Column="3" Text="{Binding ITOT2O2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}"  Margin="0"/>
                                    <TextBox x:Name="ITOT3O2" Grid.Column="4"  BorderThickness="0"  Text="{Binding ITOT3O2, UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource GridTexboxStyle}" Margin="0"/>
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                       
                    </DataGridTemplateColumn>
                    
                    <DataGridTemplateColumn  Header="DC1&#x000A;KW&#x000A;(0-10KW)" Width="*">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style  TargetType="DataGridColumnHeader">
                                <Setter Property="Height" Value="100"/>
                                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                <Setter Property="VerticalAlignment" Value="Top"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox x:Name="txtboxdc1" Text="{Binding DCurrent1, UpdateSourceTrigger=PropertyChanged}"  BorderThickness="0"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn  Header="DC2&#x000A;KW&#x000A;(0-10KW)" Width="*">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style  TargetType="DataGridColumnHeader">
                                <Setter Property="Height" Value="100"/>
                                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                <Setter Property="VerticalAlignment" Value="Top"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox x:Name="txtboxdc2" Text="{Binding DCurrent2, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0">
                                    
                                </TextBox>

                            </DataTemplate>
                            
                        </DataGridTemplateColumn.CellTemplate>
                        <!--<DataGridTemplateColumn.CellEditingTemplateSelector>
                            <Style TargetType="TextBox">
                                <Setter Property="VerticalContentAlignment" Value="Center" />
                                <Setter Property="HorizontalContentAlignment" Value="Right" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Height" Value="22" />
                            </Style>
                        </DataGridTemplateColumn.CellEditingTemplateSelector>-->
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Speed&#x000A;m/min&#x000A;(0-4)" Binding="{Binding  Speed}" Width="1*"/>

                    <DataGridTextColumn Header="StableTime&#x000A;S&#x000A;0-60秒" Binding="{Binding  StableTime}" Width="1*"/>


                </DataGrid.Columns>
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Height" Value="30"></Setter>
                        <Style.Triggers>
                            <Trigger Property="DataGridCell.IsSelected" Value="True">
                                <Setter Property="Background" Value="White" />
                                <Setter Property="Foreground" Value="Black" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
               
                <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <!--<Setter Property="FontSize" Value="30"/>-->
                        <Setter Property="Height" Value="100"/>
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                        <Setter Property="VerticalAlignment" Value="Top"/>
                    </Style>
                </DataGrid.ColumnHeaderStyle>

            </DataGrid>
            <!-- 这里是加热-->

 

我在后台获取 Name为txtboxrf1  的TexBox 的控件,  (Name为txtboxrf1  的TexBox 的控件  实际存在雨 datagrid的 Columns  2 列内)

 

 

我后台获取该控件的方法 

 

 /// 获取控件
        /// </summary>
        /// <param name="control_name">控件名</param>
        /// <param name="row">行</param>
        /// <param name="col">列</param>
        private void setStar(string control_name, int row, int col)
        {
            //首先获取DataGridTemplateColumn所在列
            DataGridTemplateColumn templeColumn = dgv.Columns[col] as DataGridTemplateColumn;
            //然后获取DataGridTemplateColumn单元格元素
            FrameworkElement s = dgv.Columns[col].GetCellContent(dgv.Items[row]);

            if (s != null)
            {
                
                System.Windows.Controls.TextBox tbOper = templeColumn.CellTemplate.FindName(control_name,s) as System.Windows.Controls.TextBox;
                tbOper.IsReadOnly = true;
            }


           
        }



 

 

 

每行获取   setStar("txtboxrf1", i,2);

 

 

 

...全文
594 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

8,750

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧