自动展开 RadGridView 新增行的 RowDetails
Automatically Expand RowDetails of Newly Added Rows of a RadGridView
我要解决的问题:
当新项目添加到 RadGridView 时,相应地创建新行。我希望这些新行自动展开 RowDetails。
到目前为止我尝试了什么:
我尝试从代码隐藏访问 RadGridView 并注册一个侦听新项目的事件处理程序。它会将新项目标记为选中,从而展开 RowDetails(RowDetailsVisibilityMode 是 VisibleWhenSelected)。
我面临的问题:
RadGridView 位于另一个控件资源的数据模板内。如果我为数据模板设置显式x:Key
,我可以通过代码访问数据模板;但是,如果我只为数据模板设置 DataType
,则资源中的每个字典条目都有一个空值。
问题是,如果我确实为数据模板设置了显式 x:Key
,我将无法再让我的控件根据数据类型动态决定使用哪个数据模板(未选择数据模板) ,我只看到一大片空白 space)。如何获取数据模板中的控件?
这是我的代码:
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
class 是 Wrapper1
和 Wrapper2
继承的抽象 class,并且 Wrappers
路径在我的视图模型上是一个 WrapperCollection
,它是一个抽象 class,继承自 ObservableCollection<Wrapper>
,Wrapper1Collection
和 Wrapper2Collection
继承自
好的,根据您的评论,我不会回答“如何访问 [...] 资源 [...]?”,但会尝试给出以下答案"如何自动展开RadGridView新增行的RowDetails?"。
我为您的 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();
}
}
我要解决的问题: 当新项目添加到 RadGridView 时,相应地创建新行。我希望这些新行自动展开 RowDetails。
到目前为止我尝试了什么: 我尝试从代码隐藏访问 RadGridView 并注册一个侦听新项目的事件处理程序。它会将新项目标记为选中,从而展开 RowDetails(RowDetailsVisibilityMode 是 VisibleWhenSelected)。
我面临的问题:
RadGridView 位于另一个控件资源的数据模板内。如果我为数据模板设置显式x:Key
,我可以通过代码访问数据模板;但是,如果我只为数据模板设置 DataType
,则资源中的每个字典条目都有一个空值。
问题是,如果我确实为数据模板设置了显式 x:Key
,我将无法再让我的控件根据数据类型动态决定使用哪个数据模板(未选择数据模板) ,我只看到一大片空白 space)。如何获取数据模板中的控件?
这是我的代码:
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
class 是 Wrapper1
和 Wrapper2
继承的抽象 class,并且 Wrappers
路径在我的视图模型上是一个 WrapperCollection
,它是一个抽象 class,继承自 ObservableCollection<Wrapper>
,Wrapper1Collection
和 Wrapper2Collection
继承自
好的,根据您的评论,我不会回答“如何访问 [...] 资源 [...]?”,但会尝试给出以下答案"如何自动展开RadGridView新增行的RowDetails?"。
我为您的 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();
}
}