我要解决的问题:
将新项目添加到RadGridView后,将相应地创建新行。我希望这些新行自动具有扩展的RowDetails。

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

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

问题是,如果我确实为数据模板设置了明确的x:Key,我将无法再让控件根据数据类型动态决定要使用哪个数据模板(未选择任何数据模板,而我看到的只是一个很大的空格处)。如何获得数据模板中的控件?

这是我的代码:

XAML:

<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#代码隐藏:

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继承的抽象类,而我的viewmodel上的Wrappers路径是WrapperCollection,这是从ObservableCollection<Wrapper>继承的抽象类,以及哪个Wrapper1CollectionWrapper2Collection继承。

最佳答案

好的,根据您的评论,我不会回答“如何访问[...]资源?”。但是尝试给出“如何自动扩展RadGridView的新添加行的RowDetails?”的答案。

我为您的Behavior推荐了一个自定义的RadGridView,它可以监听新添加的行并执行RowDetail扩展。

<RadGridView>
    <Interaction.Behaviors>
        <ExpandDetailsOfNewRowsBehavior/>
    </Interaction.Behaviors>
</RadGridView>


和代码:

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();
    }
}

10-08 19:54