Silverlight DataGrid:使用 VisualStateManager 隐藏列

发布于 2024-08-25 08:30:34 字数 2048 浏览 4 评论 0原文

是否可以在不使用代码隐藏的情况下隐藏数据网格的列? 例如通过使用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>

调用以下命令应该会隐藏该列,但这不起作用。

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戴着白色围巾的女孩 2024-09-01 08:30:35

到目前为止我还无法想出一个简单的解决方案。然而,公平的是,我至少告诉你为什么这不起作用。为了对属性进行动画处理,该属性需要是DependencyPropertyDataGridColumnVisibility 属性不是 DependencyProperty,因此它不会产生动画。

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.

离线来电— 2024-09-01 08:30:35

您可以尝试设置列宽=0

You can try setting column width = 0

北恋 2024-09-01 08:30:35

您可以子类化 DataGrid 或创建附加属性来切换可见性。但是,与不透明度不同,除非在 VisualStateManager 中启用 FluidLayout,否则您无法真正为可见性“设置动画”。

有关流畅 UI 的更多信息,请查看 http://www.microsoft .com/design/toolbox/tutorials/fluidui/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文