首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在window 10 uwp中折叠ExpandPanel

如何在window 10 uwp中折叠ExpandPanel
EN

Stack Overflow用户
提问于 2016-08-23 13:31:04
回答 1查看 864关注 0票数 0

我使用ExpandPanel来展开和折叠列表,如下所示。

代码语言:javascript
复制
 <local:ExpandPanel x:Name="test1" HeaderContent="Test1" Foreground="White" Margin="10,0,0,0" IsExpanded="False">
                                                <local:ExpandPanel.Content>
                                                    <TextBlock x:Name="yearlyAstrology" TextWrapping="Wrap"></TextBlock>
                                                </local:ExpandPanel.Content>
                                            </local:ExpandPanel>

    <local:ExpandPanel HeaderContent="Test2" Foreground="White" Margin="10,0,0,0" IsExpanded="False">
                                                <local:ExpandPanel.Content>
                                                    <TextBlock x:Name="guruAstrology" TextWrapping="Wrap"></TextBlock>
                                                </local:ExpandPanel.Content>
                                            </local:ExpandPanel>

现在,我想一次展开一个ExpandPanel。但在这种情况下,如果我展开一个面板并单击下一个面板,第一个面板不会折叠。因此,两个面板都仅处于展开状态。这看起来很烦人。我已经在C#端尝试过,如下所示。

代码语言:javascript
复制
test1.IsExpanded=false;

但它不起作用。我使用Expandanel.cs类,如下所示。

代码语言:javascript
复制
public ExpandPanel()
        {
            this.DefaultStyleKey = typeof(ExpandPanel);
        }

        private bool _useTransitions = true;
        private VisualState _collapsedState;
        private Windows.UI.Xaml.Controls.Primitives.ToggleButton toggleExpander;
        private FrameworkElement contentElement;

        public static readonly DependencyProperty HeaderContentProperty =
        DependencyProperty.Register("HeaderContent", typeof(object),
        typeof(ExpandPanel), null);

        public static readonly DependencyProperty IsExpandedProperty =
        DependencyProperty.Register("IsExpanded", typeof(bool),
        typeof(ExpandPanel), new PropertyMetadata(true));

        public static readonly DependencyProperty CornerRadiusProperty =
        DependencyProperty.Register("CornerRadius", typeof(CornerRadius),
        typeof(ExpandPanel), null);

        public object HeaderContent
        {
            get { return GetValue(HeaderContentProperty); }
            set { SetValue(HeaderContentProperty, value); }
        }

        public bool IsExpanded
        {
            get { return (bool)GetValue(IsExpandedProperty); }
            set { SetValue(IsExpandedProperty, value); }
        }

        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        private void changeVisualState(bool useTransitions)
        {
            if (IsExpanded)
            {
                if (contentElement != null)
                {
                    contentElement.Visibility = Visibility.Visible;
                }
                VisualStateManager.GoToState(this, "Expanded", useTransitions);
            }
            else
            {
                VisualStateManager.GoToState(this, "Collapsed", useTransitions);
                _collapsedState = (VisualState)GetTemplateChild("Collapsed");
                if (_collapsedState == null)
                {
                    if (contentElement != null)
                    {
                        contentElement.Visibility = Visibility.Collapsed;
                    }
                }
            }
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            toggleExpander = (Windows.UI.Xaml.Controls.Primitives.ToggleButton)
                GetTemplateChild("ExpandCollapseButton");
            if (toggleExpander != null)
            {
                toggleExpander.Click += (object sender, RoutedEventArgs e) =>
                {
                    IsExpanded = !IsExpanded;
                    toggleExpander.IsChecked = IsExpanded;
                    changeVisualState(_useTransitions);
                };
            }
            contentElement = (FrameworkElement)GetTemplateChild("Content");
            if (contentElement != null)
            {
                _collapsedState = (VisualState)GetTemplateChild("Collapsed");
                if ((_collapsedState != null) && (_collapsedState.Storyboard != null))
                {
                    _collapsedState.Storyboard.Completed += (object sender, object e) =>
                    {
                        contentElement.Visibility = Visibility.Collapsed;
                    };
                }
            }
            changeVisualState(false);
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-23 15:56:35

在给定的类中,IsExpanded属性在更改时不会影响任何内容。您可以在依赖项属性的定义中添加回调,以在属性更改时更改视觉状态:

代码语言:javascript
复制
public static readonly DependencyProperty IsExpandedProperty = 
    DependencyProperty.Register("IsExpanded", typeof(bool), typeof(ExpandPanel), 
        new PropertyMetadata(true, IsExpanded_Changed));

private static void IsExpanded_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    var panel = (ExpandPanel)sender;
    panel.toggleExpander.IsChecked = IsExpanded;
    panel.changeVisualState(false);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39093030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档