views:

43

answers:

1

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  A: 

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.

AnthonyWJones
Thanks for the input, makes sence. THe solution ive been working with, is to inherate the DataGrid an implement a DependencyProperty which can control the visibilty of the columns. Ill get around posting this solution later. If you got a better one, please let me know :-)
Lars Udengaard