在Silverlight DataGrid中展开折叠按钮

在Silverlight DataGrid中展开折叠按钮,第1张

概述我在Silverlight DataGrid中使用RowDetailsTemplate来显示行详细信息.设置RowDetailsVisibilityMode =“VisibleWhenSelected”不能提供良好的用户体验(一次只能扩展一行,所有行都不能折叠).在每行上添加展开/折叠按钮的最简单方法是什么,以便可以独立展开/折叠行? 我一直想写我的解决方案. 我将网格RowDetailsVisi 我在Silverlight DataGrID中使用RowDetailstemplate来显示行详细信息.设置RowDetailsVisibilityMode =“VisibleWhenSelected”不能提供良好的用户体验(一次只能扩展一行,所有行都不能折叠).在每行上添加展开/折叠按钮的最简单方法是什么,以便可以独立展开/折叠行?解决方法 我一直想写我的解决方案.
我将网格RowDetailsVisibilityMode设置为Collapsed并使用带有样式化Togglebutton的DataGrIDTemplateColumn来切换行可见性.

可以使用绑定或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         });    }

对于TriggerAction方法:

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>
总结

以上是内存溢出为你收集整理的在Silverlight DataGrid中展开/折叠按钮全部内容,希望文章能够帮你解决在Silverlight DataGrid中展开/折叠按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1209337.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存