首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动展开新添加的RadGridView行的RadGridView

自动展开新添加的RadGridView行的RadGridView
EN

Stack Overflow用户
提问于 2015-05-27 21:21:37
回答 1查看 1.1K关注 0票数 1

我想解决的问题:当新项被添加到RadGridView中时,相应地创建新行时的。我希望这些新行自动展开RowDetails。

到目前为止我尝试过的:,我尝试从代码背后访问RadGridView,并注册一个侦听新项目的事件处理程序。它将将新项目标记为选定的项,从而展开RowDetails (RowDetailsVisibilityMode is VisibleWhenSelected)。

我面临的问题: RadGridView位于另一个控件的资源的数据模板中。如果我为数据模板设置了显式的x:Key,我可以通过代码访问数据模板;但是,如果我只为数据模板设置了一个DataType,那么资源中的每个字典条目都有一个空值。

问题是,如果我确实为数据模板设置了显式的x:Key,我就不能再让我的控件根据数据类型动态地决定使用哪个数据模板(没有选择数据模板,我只看到一个大的空白)。如何获得数据模板中的控件?

这是我的密码:

XAML:

代码语言:javascript
复制
<telerik:RadTabControl x:Name="radTabControl">
    <telerik:RadTabControl.Resources>
        <DataTemplate x:Key="TabControlTemplate">
            <ContentControl Content="{Binding}">
                <ContentControl.Resources>
                    <!-- Wrapper1 (inherits from Wrapper) -->
                    <DataTemplate DataType="local:Wrapper1Collection">
                        <telerik:RadGridView ItemsSource="{Binding}">
                        ....
                        </telerik:RadGridView>
                    </DataTemplate>

                    <!-- Wrapper2 (inherits from Wrapper) -->
                    <DataTemplate DataType="local:Wrapper2Collection">
                        <telerik:RadGridView ItemsSource="{Binding}">
                        ....
                        </telerik:RadGridView>
                    </DataTemplate>

                    <!-- Fallback to displaying nothing for unknown wrapper types -->
                    <DataTemplate DataType="local:WrapperCollection" />
                </ContentControl.Resources>
            </ContentControl>
        </DataTemplate>
    </telerik:RadTabControl.Resources>

    <telerik:RadTabItem Content="{Binding Path=Wrappers}"
                        ContentTemplate="{StaticResource TabControlTemplate}" />
    <telerik:RadTabItem Content="{Binding Path=Wrappers}"
                        ContentTemplate="{StaticResource TabControlTemplate}" />
</telerik:RadTabControl>

C#代码-隐藏:

代码语言:javascript
复制
public ContentUpdateView()
{
    InitializeComponent();

    DataTemplate tabControlTemplate =
        (DataTemplate)(radTabControl.Resources["TabControlTemplate"]);
    FrameworkElement radGridViewContentControl =
        (FrameworkElement)(tabControlTemplate.LoadContent());

    foreach (DictionaryEntry resourceEntry in radGridViewContentControl.Resources)
    {
        if (resourceEntry.Value != null)
        {
            DataTemplate radGridViewDataTemplate = (DataTemplate)(resourceEntry.Value);
            RadGridView radGridView = (RadGridView)(radGridViewDataTemplate.LoadContent());

            radGridView.Items.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (Wrapper wrapper in e.NewItems)
                    {
                        radGridView.SelectedItems.Add(wrapper);
                    }
                }
            };
        }
    }
}

请注意,Wrapper类是Wrapper1Wrapper2继承的抽象类,而在我的视图模型中的Wrappers路径是WrapperCollection,它是从ObservableCollection<Wrapper>继承的抽象类,Wrapper1CollectionWrapper2Collection继承的抽象类。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-01 14:47:27

好吧,基于你的评论,我不会回答“如何访问.资源.”但试着回答“如何自动展开新添加的RowDetails行的RadGridView?”。

我为您的RadGridView重新推荐了一个自定义RadGridView,它侦听新添加的行并执行RowDetail展开。

代码语言:javascript
复制
<RadGridView>
    <Interaction.Behaviors>
        <ExpandDetailsOfNewRowsBehavior/>
    </Interaction.Behaviors>
</RadGridView>

和代码:

代码语言:javascript
复制
public class ExpandDetailsOfNewRowsBehavior : Behavior<RadGridView>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        INotifyCollectionChanged items = AssociatedObject.Items;
        items.CollectionChanged += OnItemsChanged;
        AssociatedObject.RowLoaded += CheckLoadedRowIfShouldExpand;
    }

    private void CheckLoadedRowIfShouldExpand()
    {
        var iterationCopy = m_shouldBeExpanded.ToArray();
        foreach(var item in iterationCopy)
        {
            GridViewRow row = AssociatedObject.GetRowForItem( item );
            if ( row != null )
            {
                row.DetailsVisibility = Visibility.Visible;
                m_shouldBeExpanded.Remove(item);
            }
        }
    }

    private List<object> m_shouldBeExpanded = new List<object>();

    private void OnItemsChanged(object sender, NotifyCollectionChangedEventArgs eventArgs)
    {
        if (eventArgs.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (var item in eventArgs.NewItems)
            {
              GridViewRow row = AssociatedObject.GetRowForItem( item );
              if ( row == null )
              {
                // row is not loaded yet
                m_shouldBeExpanded.Add(item);
              }
              else
              {
                row.DetailsVisibility = Visibility.Visible;
                // see http://docs.telerik.com/devtools/silverlight/
                //                  controls/radgridview/row-details/programming
                // "To manually change the visibility of a row - set its
                // DetailsVisibility property to either Visibility.Collapsed
                // or Visibility.Visible"
              }
            }
        }
    }

    protected override void OnDetaching()
    {
        INotifyCollectionChanged items = AssociatedObject.Items;
        items.CollectionChanged -= OnItemsChanged;
        AssociatedObject.RowLoaded -= CheckLoadedRowIfShouldExpand;
        base.OnDetaching();
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30493370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档