Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight数据网格中的展开/折叠按钮_Silverlight_Datagrid - Fatal编程技术网

Silverlight数据网格中的展开/折叠按钮

Silverlight数据网格中的展开/折叠按钮,silverlight,datagrid,Silverlight,Datagrid,我正在Silverlight数据网格中使用RowDetailsTemplate来显示行详细信息。设置RowDetailsVisibilityMode=“VisibleWhenSelected”无法提供良好的用户体验(一次只能展开一行,不能折叠所有行)。在每一行上添加展开/折叠按钮以使行可以独立展开/折叠的最简单方法是什么?我一直想把我的解决方案写在博客上。 我将grid RowDetailsVisibilityMode设置为Collapsed,并使用DataGridTemplateColumn(

我正在Silverlight数据网格中使用RowDetailsTemplate来显示行详细信息。设置RowDetailsVisibilityMode=“VisibleWhenSelected”无法提供良好的用户体验(一次只能展开一行,不能折叠所有行)。在每一行上添加展开/折叠按钮以使行可以独立展开/折叠的最简单方法是什么?

我一直想把我的解决方案写在博客上。 我将grid RowDetailsVisibilityMode设置为Collapsed,并使用DataGridTemplateColumn(其中带有样式化的ToggleButton)来切换行可见性

切换按钮可以通过绑定或触发操作来切换行可见性。
绑定必须在代码隐藏中完成,因为您试图将ToggleButton.IsChecked绑定到生成的元素,而该元素在XAML(DataGridRow.DetailsVisibility)中不存在 (这将允许在SL5中具有更强的相对资源绑定)

对于这两种解决方案,我在助手类中都有此扩展方法:

    /// <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;
    }
//
///向上遍历VisualTree,返回作为类型参数提供的类型的第一个父对象
/// 
公共静态T FindAncestor(此DependencyObject对象),其中T:DependencyObject
{
while(obj!=null)
{
T o=作为T的obj;
如果(o!=null)
返回o;
obj=VisualTreeHelper.GetParent(obj);
}
返回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 
        });
    }
private void ToggleButton_加载(对象发送方,路由目标)
{
ToggleButton=发送方为ToggleButton;
DataGridRow行=button.FindAncestor();//自定义扩展名
row.SetBinding(DataGridRow.DetailsVisibilityProperty,新绑定()
{   
源=按钮,
路径=新属性路径(“已检查”),
转换器=新的VisibilityConverter(),
Mode=BindingMode.TwoWay
});
}
对于触发方法:

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;
        }
    }
}
公共类ExpandRowAction:TriggerAction
{
受保护的覆盖无效调用(对象o)
{
var row=this.AssociatedObject.FindAncestor();
如果(行!=null)
{
if(this.AssociatedObject.IsChecked==true)
row.DetailsVisibility=可见性.Visible;
其他的
row.DetailsVisibility=可见性。已折叠;
}
}
}
然后在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>


谢谢。这很有魅力!我唯一的麻烦就是PlusMinusToggleButtonStyle。我创建了一个可以切换两个图像的样式,但这不起作用。为此,我将发布一个单独的问题。无耻的自我推销:在我的网站上发布博客解决方案。