需要从CodeBehind中的DataTemplate内部访问具名的DataGridTextColumn [英] Need to access a Named DataGridTextColumn from within a DataTemplate in CodeBehind

查看:62
本文介绍了需要从CodeBehind中的DataTemplate内部访问具名的DataGridTextColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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?

推荐答案

我的建议:将包含"LogStepDetails"新属性的对象添加到您的对象中(如我所见,可能为3)并将它们绑定到"可见度''.这样,您无需在代码中添加新逻辑,只需更改这些新属性的值即可处理每一列的可见性.
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.


对不起,您对.检查此解决方案:http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx
它完全可以解决您的需求.
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.


我终于找到了解决方案. Nestor的解决方案是我已经尝试过的解决方案,但对我却不起作用(老实说,我认为我有错).但是,下面的代码很好地解决了我的问题...

我捕获了DataGrid的Initialized事件,在这里我可以*访问列,然后在那里做我的工作……而这一切都超出了XAML代码.这是我的解决方案的一个例子.也许有更好的方法,但这可行.

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.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆