Button click changes visibility of a DataGrid with a Trigger in WPF

拟墨画扇 提交于 2019-12-13 05:27:11

问题


Hi i am trying to find some way to when a button is clicked changes the visibility of other control, like a DataGrid with a Trigger in XAML.

The button only changes the visibility of the DataGrid to Visible, it does other things in Code Behind, but this is something that i think that can be done in a Style with a Trigger.

I tried to find a solution and it seems to be possible to do but i can't understand how.

Thanks in advance.


回答1:


<Button Content="Button!">
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference dataGrid}"
                                                   Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0"
                                                Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Button.Triggers>
</Button>

{x:Reference dataGrid} references a DataGrid with the name dataGrid, alternatively you could just use Storyboard.TargetName. You would normally use the Storyboard.Target property if you do binding or references to resources.




回答2:


Just a suggestion, but how about, for something more understandable, having a Checkbox enabling/disabling the DataGrid display? This is what I usually do:

<DockPanel LastChildFill="True">
   <CheckBox DockPanel.Dock="Right" VerticalAlignment="Center" x:Name="DisplayBox"
                      Content="Display grid" Margin="4" IsChecked="False"/>
   <DataGrid Visibility="{Binding ElementName=DisplayBox, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}" />
</DockPanel>

And of course, you'll have to implement the appropriate converter



来源:https://stackoverflow.com/questions/5663394/button-click-changes-visibility-of-a-datagrid-with-a-trigger-in-wpf

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