我在Silverlight DataGrid中使用RowDetailsTemplate来显示行详细信息。设置RowDetailsVisibilityMode =“VisibleWhenSelected”没有提供良好的用户体验(一次只能扩展一行,所有行不能折叠)。在每一行上添加展开/折叠按钮的最简单方法是什么,以便行可以独立展开/折叠?在Silverlight DataGrid中展开/折叠按钮
5
A
回答
4
我一直在意味着博客我的解决方案。 我将网格RowDetailsVisibilityMode设置为Collapsed,并使用DataGridTemplateColumn和其中的样式化ToggleButton切换行可见性。
可以使用绑定或通过TriggerAction连接切换按钮以切换行可见性。
因为您试图将ToggleButton.IsChecked绑定到生成的元素并且不存在于XAML中(DataGridRow.DetailsVisibility) (这将允许在SL5中使用更强的RelativeSource绑定),因此绑定必须在代码隐藏中完成)
对于这两种解决方案,我有一个助手类此扩展方法:
/// <summary>
/// Walk up the VisualTree, returning first parent object of the type supplied as type parameter
/// </summary>
public static T FindAncestor<T>(this DependencyObject obj) where T : DependencyObject
{
while (obj != null)
{
T o = obj as T;
if (o != null)
return o;
obj = VisualTreeHelper.GetParent(obj);
}
return null;
}
对于代码隐藏装订方法:
private void ToggleButton_Loaded(object sender, RoutedEventArgs e)
{
ToggleButton button = sender as ToggleButton;
DataGridRow row = button.FindAncestor<DataGridRow>(); //Custom Extension
row.SetBinding(DataGridRow.DetailsVisibilityProperty, new Binding()
{
Source = button,
Path = new PropertyPath("IsChecked"),
Converter = new VisibilityConverter(),
Mode = BindingMode.TwoWay
});
}
对于特里格erAction方法:
public class ExpandRowAction : TriggerAction<ToggleButton>
{
protected override void Invoke(object o)
{
var row = this.AssociatedObject.FindAncestor<DataGridRow>();
if (row != null)
{
if (this.AssociatedObject.IsChecked == true)
row.DetailsVisibility = Visibility.Visible;
else
row.DetailsVisibility = Visibility.Collapsed;
}
}
}
然后在XAML:
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ToggleButton Style="{StaticResource PlusMinusToggleButtonStyle}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<behaviors:ExpandRowAction/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ToggleButton>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
相关问题
- 1. 使用祖先RelativeSource绑定的Silverlight 5 DataGrid中的展开/折叠按钮
- 2. 在WPF DataGrid上展开/折叠行
- 3. wpf按钮折叠/展开动作
- 4. 隐藏展开/折叠+/-按钮
- 5. 折叠时移动按钮展开
- 6. 通过按钮展开和折叠行
- 7. 展开/折叠包含DataGrid的面板
- 8. 在Datagridview中展开/折叠
- 9. 展开/折叠jQuery
- 10. 折叠/展开Groupboxes
- 11. 展开/折叠Div
- 12. jquery展开/折叠?
- 13. 折叠/展开optgroups
- 14. 重复使用TreeView的展开[+]并折叠WPF中的[ - ]按钮
- 15. PHP foreach循环中的多个展开/折叠引导按钮
- 16. 如何使用MVVM折叠Silverlight DataGrid组?
- 17. 使用HierarchicalDataTemplate在Silverlight TreeView中自定义展开/折叠符号
- 18. 折叠按钮4
- 19. Silverlight 3 DataGrid分组 - 检测组标题单击或标题展开/折叠
- 20. jQuery - 展开/折叠所有按钮,Internet Explorer问题
- 21. 抽奖展开/折叠按钮(+/-)时Aero是关闭
- 22. 是否可以从jqGrid头部删除展开/折叠按钮?
- 23. 的WinForms:做一个折叠/展开图像按钮
- 24. 展开折叠菜单上的引导检查单选按钮
- 25. 导航栏折叠按钮只能展开(引导)
- 26. 树视图与展开和折叠按钮优秀
- 27. 通过按钮展开/折叠桌子上的所有行
- 28. Android:ActionView折叠按回按钮
- 29. 如何展开 - 在Swift中折叠UITextView?
- 30. 在RecyclerView中展开和折叠卡
感谢。这就像一个魅力!我遇到的麻烦是PlusMinusToggleButtonStyle。我创建了切换两张图像的样式,但这不起作用。我将为此发布一个单独的问题。 – Naresh 2011-03-09 03:37:26
无耻自我推销:在我的网站上发布博客解决方案[mikeherman.net](http://mikeherman.net/blog/workarounds-for-weaker-relativesource-binding-in-silverlight-4)。 – foson 2011-03-28 00:32:48