自动展开RadGridView新增行的RowDetails
本文关键字:新增行 RowDetails RadGridView | 更新日期: 2023-09-27 17:51:02
我想解决的问题:当向RadGridView添加新项时,将相应地创建新行。我希望这些新行自动扩展RowDetails。
我试过了:我尝试从代码后端访问RadGridView,并注册一个监听新项目的事件处理程序。它会将新条目标记为selected,从而展开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
类是Wrapper1
和Wrapper2
继承的抽象类,并且我的视图模型上的Wrappers
路径是WrapperCollection
,这是一个继承自ObservableCollection<Wrapper>
的抽象类,Wrapper1Collection
和Wrapper2Collection
继承自。
好吧,根据你的评论,我不会回答"如何访问[…][…]?",但试图给出一个答案,"如何自动展开RowDetails的新添加行的RadGridView?"。
我推荐为您的RadGridView
定制Behavior
,它侦听新添加的行并执行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();
}
}