我正在使用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);
}
答案 0 :(得分:1)
在给定的课程中,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);
}