首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

Silverlight动态获取GridView的 列模板DataTemplate内控件解决方案

2012-12-31 
Silverlight动态获取GridView的 列模板DataTemplate内控件一个GridView的GridViewColumn定制多层表头Heade

Silverlight动态获取GridView的 列模板DataTemplate内控件
  一个GridView的GridViewColumn定制多层表头HeaderCellStyle,并定制对应的模板DataTemplate,
  提供当前GridView对象,
  1,获取模板列DataTemplate里面的控件(条件:并不知道控件的名称)
  2,获取多层表头HeaderCellStyle的层次和每层的控件(这个貌似无法实现)
  请大家提供实现思路


多行表头样式---
<Style x:Key="AuditResultHeaderStyle" TargetType="dataprimitives:GridViewHeaderCell" >
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid x:Name="Root">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="1" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="90" />
                                    <ColumnDefinition Width="1" />
                                    <ColumnDefinition Width="90" />
                                    <ColumnDefinition Width="1" />
                                    <ColumnDefinition Width="90" />
                                    <ColumnDefinition Width="1" />


                                </Grid.ColumnDefinitions>
                                <TextBlock Text="第一次稽核结果标注汇总" Grid.Row="0" Grid.ColumnSpan="5" Margin="0,4,0,4" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                <Rectangle Fill="#FFC9CACA" Grid.Row="0" Grid.Column="5" HorizontalAlignment="Stretch"/>
                                <Rectangle Fill="#FFC9CACA" Grid.Row="1" Grid.ColumnSpan="6" HorizontalAlignment="Stretch"/>
                                <TextBlock Text="无问题数" Grid.Row="2"  Grid.Column="0" HorizontalAlignment="Center" Margin="0,4,0,4" VerticalAlignment="Center"/>
                                <Rectangle Fill="#FFC9CACA" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch"/>
                                <TextBlock Text="疑点及问题数" Grid.Row="2" Grid.Column="2" Margin="0,4,0,4" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                <Rectangle Fill="#FFC9CACA" Grid.Row="2" Grid.Column="3" HorizontalAlignment="Stretch"/>
                                <TextBlock Text="疑点及问题金额" Grid.Row="2" Grid.Column="4" Margin="0,4,0,4" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                <Rectangle Fill="#FFC9CACA" Grid.Row="2" Grid.Column="5" HorizontalAlignment="Stretch"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
列定义(引用上的HeaderCellStyle,并定制对应的模板DataTemplate)----
<telerikGridView:GridViewDataColumn HeaderTextAlignment="Left" 


                                                        HeaderCellStyle="{StaticResource AuditResultHeaderStyle}">
                    <telerikGridView:GridViewDataColumn.CellTemplate >
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="83 " />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="85" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="85" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{Binding notErrorCount}" VerticalAlignment="Center" TextAlignment="Right"/>
                                <Rectangle Grid.Column="1" Fill="#FFC9CACA" VerticalAlignment="Stretch" Width="1" MinHeight="25"/>
                                <TextBlock Grid.Column="2" Text="{Binding errorAndQuestionCount}" VerticalAlignment="Center" TextAlignment="Right"/>
                                <Rectangle Grid.Column="3" Fill="#FFC9CACA" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="1" MinHeight="25"/>


                                <TextBlock Grid.Column="4" Text="{Binding errorAndQuestionMoneyTotal, StringFormat=C,ConverterCulture=zh-CN}" VerticalAlignment="Center" TextAlignment="Right"/>
                            </Grid>
                        </DataTemplate>
                    </telerikGridView:GridViewDataColumn.CellTemplate>
                </telerikGridView:GridViewDataColumn>


[解决办法]
有一个比较笨的办法是直接使用FindChildern函数试试


        #region 查找某种类型的子孙节点
        /// <summary>
        /// 查找某种类型的子孙节点
        /// </summary>
        /// <typeparam name="T">目标节点类型</typeparam>
        /// <param name="parent">起始节点</param>
        /// <returns>符合条件的节点集合</returns>
        public static IEnumerable<T> FindChildren<T>(this DependencyObject parent) where T : class
        {
            var count = VisualTreeHelper.GetChildrenCount(parent);
            if (count > 0)
            {
                for (var i = 0; i < count; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    var t = child as T;
                    if (t != null)
                        yield return t;

                    var children = FindChildren<T>(child);
                    foreach (var item in children)
                        yield return item;
                }


            }
        }



[解决办法]
问题问的好,专家答的也好。
[解决办法]
顶一个
[解决办法]
学习了

热点排行