Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...

XML
<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?
Posted

My suggestion: Add to your object holding the 'LogStepDetails' new properties (maybe 3 as I can see) and bind them to the 'Visibility' of each column. This way you don't need to add new logic to your code and just change the values of those new properties to handle every column's visibility.
 
Share this answer
 
Comments
MGScott 13-Jun-12 18:05pm    
I've actually tried that.... doesn't seem to work. I had something like :

Visibility="{Binding Path=GridVisibilitySetting}" added to the mix, and it's not finding it.
Sorry, you're right. Check this solution: http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx
it resolves exactly what you need.
 
Share this answer
 
Comments
Sandeep Mewara 14-Jun-12 16:43pm    
You can use 'Improve Solution' and update your own answer at any point of time.
I finally found a solution. Nestor's solution is something I've already tried, and it did not work for me (I think I was at fault, to be honest though). However, the below resolved my problem nicely...

I trapped the Initialized event for the DataGrid, where I *could* get access to the columns, and did my work there... and it's ALL out of the XAML code. Here is an example of my solution. There may be a better way, but this works.

C#
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 );
    }
}
 
Share this answer
 
I've actually tried that.... doesn't seem to work. I had something like :

Visibility="{Binding Path=GridVisibilitySetting}"

added to the mix, and it's not finding it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900