问题描述
我有一个DataGrid,其中包含一组使用x:Name命名的DataGridTextColumns.一旦将该DataGrid包裹在Dataemplate中,就无法再在后面的代码中访问这些DataGridTextColumns了.我需要再次访问它们,因为我需要以编程方式设置每列的可见性".有什么办法可以访问它们?这是XAML的当前结构.
我确实尝试过与设置Header相似的方法-指向父类中的属性,但这也不起作用...
I have a DataGrid that contains a group of DataGridTextColumns named using x:Name. Once I wrapped that DataGrid inside a Dataemplate, I can no longer access these DataGridTextColumns in the Code-behind. I need to gain access to them again, as I need to programmatically set each column''s Visibility. Is there any way to access them? Here''s the current structure of the XAML.
I did try something similar to how I set the Header - pointing to a property in the parent class, but that isn''t working either...
<StackPanel Visibility="{Binding Path=ML300LogFileVisibility}">
<ItemsControl ItemsSource="{Binding Path=ParentController.Logs}">
<ItemsControl.ItemTemplate>
<DataTemplate x:Name="dataTemplateML300" DataType="{x:Type src:LogFile}">
<Expander Style="{StaticResource StatusGroupExpander}" Header="{Binding Path=MethodName}">
<DataGrid x:Name="dataGidML300" Visibility="Visible" CanUserReorderColumns="True" Margin="0,10,0,0"
IsReadOnly="True" HeadersVisibility="Column" CanUserSortColumns="False"
AutoGenerateColumns="False" ItemsSource="{Binding Path=LogStepDetails}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="ColTitleML300Step" HeaderStyle="{StaticResource GridHeader}" Binding="{Binding Path=StepNumber}"
Header="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource ResourceKey=StringConverter}, ConverterParameter=step}">
</DataGridTextColumn>
<DataGridTextColumn x:Name="ColTitleML300StartTime" HeaderStyle="{StaticResource GridHeader}" Binding="{Binding Path=ParentTask.StartTime}"
Header="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource ResourceKey=StringConverter}, ConverterParameter=txtStartTime}">
</DataGridTextColumn>
<DataGridTextColumn x:Name="ColTitleML300Duration" HeaderStyle="{StaticResource GridHeader}" Binding="{Binding Path=ParentTask.Duration}"
Header="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource ResourceKey=StringConverter}, ConverterParameter=Duration}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
Suggestions?
推荐答案
private void dataGidML300_Initialized( object sender, EventArgs e )
{
try
{
DataGrid dg = sender as DataGrid;
foreach( DataGridTextColumn col in dg.Columns )
{
string strColName = col.Header.ToString( );
// Shared Column Headers
if( strColName == mStrings.GetStringWithThreadCulture( "txtStep" ) )
col.Visibility = this.GridStep;
else if( strColName == mStrings.GetStringWithThreadCulture( "txtStartTime" ) )
col.Visibility = this.GridStartTime;
else if( strColName == mStrings.GetStringWithThreadCulture( "txtDuration" ) )
col.Visibility = this.GridDuration;
// New Log Type Column Headers
else if( strColName == mStrings.GetStringWithThreadCulture( "txtStepType" ) )
col.Visibility = this.GridStepType;
else if( strColName == mStrings.GetStringWithThreadCulture( "txtFluidVol" ) )
col.Visibility = this.GridFluidVolume;
else if( strColName == mStrings.GetStringWithThreadCulture( "txtFlowRate" ) )
col.Visibility = this.GridFlowRate;
else if( strColName == mStrings.GetStringWithThreadCulture( "txtValvePos" ) )
col.Visibility = this.GridValvePosition;
else if( strColName == mStrings.GetStringWithThreadCulture( "txtTipSize" ) )
col.Visibility = this.GridTipSize;
else if( strColName == mStrings.GetStringWithThreadCulture( "txtLiquidClass" ) )
col.Visibility = this.GridLiquidClass;
}
}
catch( Exception ex )
{
Errors.ErrorMessage( ex, new StackFrame( ), true );
mLog.Error( ex.Message, ex );
}
}
这篇关于需要从CodeBehind中的DataTemplate内部访问具名的DataGridTextColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!