Silverlight DataGrid: Hiding columns using VisualStateManager

早过忘川 提交于 2019-12-11 13:29:00

问题


Is it possible to hide a column of a datagrid, without using codebehind? E.g. by using the VisualStateManager?

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
x:Class="Buttons.MainPage"
Width="640" Height="480">

<StackPanel x:Name="LayoutRoot" Width="624" HorizontalAlignment="Right" Margin="0,0,8,0" >
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="EditStates">
            <VisualState x:Name="ReadOnly" />
            <VisualState x:Name="Edit">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ShownInEditMode" Storyboard.TargetProperty="(UIElement.Visibility)" BeginTime="00:00:00" Duration="00:00:00.0010000">
                        <DiscreteObjectKeyFrame KeyTime="00:00:00">
                            <DiscreteObjectKeyFrame.Value>
                                <Visibility>Visible</Visibility>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <data:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding BBRNumbers}">
        <data:DataGrid.Columns>
            <data:DataGridTextColumn Header="AlwaysShown" Width="80" Binding="{Binding Municipality}" />
            <data:DataGridTextColumn Header="ShownInEditMode" Width="73" Binding="{Binding Estate}" Visibility="Collapsed" />
        </data:DataGrid.Columns>
    </data:DataGrid>
</StackPanel>

Calling the following should then hide the column, but this doesnt work.

VisualStateManager.GoToState(this, "Edit", false);

Any ideas?


回答1:


I haven't been able to come up with a simple solution to this as yet. However its only fair that I at least tell you why this isn't working. In order to animate a property the property needs to be DependencyProperty. The Visibility property of the DataGridColumn is not a DependencyProperty, hence it does not animate.




回答2:


You can try setting column width = 0




回答3:


You can either subclass DataGrid or create an attached property to toggle Visibility. However, unlike Opacity, you can't really 'animate' Visibility unless you enable FluidLayout in the VisualStateManager.

For more info regarding the fluid UI, please take a look at http://www.microsoft.com/design/toolbox/tutorials/fluidui/



来源:https://stackoverflow.com/questions/2447690/silverlight-datagrid-hiding-columns-using-visualstatemanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!