可以在Silverlight / WPF中为DataGrid列折叠设置动画吗?

时间:2011-10-24 13:40:46

标签: wpf silverlight datagrid visualstatemanager

我在Silverlight中使用此列模板有一个简单的DataGrid

<data:DataGrid AutoGenerateColumns="False" ...>

    <data:DataGridTextColumn x:Name="idCol" Header="Id" Binding="{Binding Id}" />

    <data:DataGridTextColumn x:Name="nameCol" Header="Name" Binding="{Binding Name}" />

    <data:DataGridTextColumn x:Name="descriptionCol" Header="Description" Binding="{Binding Description}" />

</data:DataGrid

我希望以动画方式折叠和展开一些列。那可能吗?因为我不能在列宽模板上使用VSM而且列宽不是DP,所以它是DataGridLength

private void ExpandColumns()
{
    // not animated expanding
    //myDataGrid.Columns[0].Visibility = Visibility.Visible;
    //myDataGrid.Columns[2].Visibility = Visibility.Visible;.

}

private void CollapseColumns()
{
    // not animated collapsing
    //myDataGrid.Columns[0].Visibility = Visibility.Collapsed;
    //myDataGrid.Columns[2].Visibility = Visibility.Collapsed;

}

1 个答案:

答案 0 :(得分:2)

可能有些骇人听闻,但如何在列上设置MinWidthMaxWidth的动画效果呢?基本上根据您要执行的操作设置它们,并将MinWidth设置为更高的值以进行展开,并将MaxWidth设置为较低的值以进行折叠。

这是在MinWidth上制作动画的完整xaml(我仍然认为这有点像hackish):

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SolveProblems.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
    <Storyboard x:Key="OnClick1">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(DataGridColumn.MinWidth)" Storyboard.TargetName="dataGridCheckBoxColumn">
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="80"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(DataGridColumn.MinWidth)" Storyboard.TargetName="dataGridCheckBoxColumn1">
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="90"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>
<Window.Triggers>
    <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button">
        <BeginStoryboard Storyboard="{StaticResource OnClick1}"/>
    </EventTrigger>
</Window.Triggers>
<Grid x:Name="LayoutRoot">
    <DataGrid x:Name="dataGrid" Margin="100,54,129.5,82.5">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn x:Name="dataGridCheckBoxColumn" Width="{Binding Columns[0].MinWidth, ElementName=dataGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridCheckBoxColumn x:Name="dataGridCheckBoxColumn1" Width="{Binding Columns[1].MinWidth, ElementName=dataGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Right" Margin="0,115,22,0" VerticalAlignment="Top" Width="75"/>

    </Grid>