我使用ExpandPanel来展开和折叠列表,如下所示。
<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#端尝试过,如下所示。
test1.IsExpanded=false;但它不起作用。我使用Expandanel.cs类,如下所示。
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);
}发布于 2016-08-23 15:56:35
在给定的类中,IsExpanded属性在更改时不会影响任何内容。您可以在依赖项属性的定义中添加回调,以在属性更改时更改视觉状态:
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);
}https://stackoverflow.com/questions/39093030
复制相似问题