手风琴项目不处于正确的视觉状态
我需要使用 Accordion 在我们正在构建的 LOB 应用程序上显示一些总计。
如果我将 Accordion 放在 XAML 中,则一切正常,并且图标 (>) 的状态正确并指向右侧。当鼠标进入 AccordionItem 时,我们没有看到视觉状态发生变化。
如果我在按钮单击上动态添加 AccordionItems(以模拟异步数据调用返回),则图标的状态不相同,并且在 MouseEnter 上,它通过执行视觉状态更改来“纠正”自身。 *您可能需要单击“添加 3 个手风琴项目”两次。
如果我使用 AccordionItems 在 Button 单击上动态添加 Accordion,则效果很好。下面是我的示例应用程序。
那么我需要做什么才能让 Accordion 在运行时添加 AccordionItems 并在使用 XAML 时处于正确的状态?
XAML
<Grid x:Name="LayoutRoot" Background="Black" >
<StackPanel x:Name="TheStackPanel">
<Button Content="Create Accordion" Click="CreateAccordionItems"></Button>
<Button Content="Add 3 Accordion Items" Click="AddAccordionItems"></Button>
<Grid Background="Pink">
<layoutToolkit:Accordion SelectionMode="ZeroOrMore" x:Name="TestAccordion" Margin="10,10,10,10" HorizontalAlignment="Stretch" >
<layoutToolkit:AccordionItem Content="Content - 1" Header="Header - 1">
</layoutToolkit:AccordionItem>
<layoutToolkit:AccordionItem Content="Content - 2" Header="Header - 2">
</layoutToolkit:AccordionItem>
<layoutToolkit:AccordionItem Content="Content - 3" Header="Header - 3">
</layoutToolkit:AccordionItem>
</layoutToolkit:Accordion>
</Grid>
</StackPanel>
public partial class MainPage : UserControl
{ 私有整数计数 = 0; 公共主页() { // 需要初始化变量 初始化组件(); //TestAccordion.ExpandDirection = ExpandDirection.Down; }
private void AddAccordionItems( object sender, RoutedEventArgs e )
{
AddToAccordion( 3, TestAccordion );
}
private void AddToAccordion( int size, Accordion _Accordion )
{
for( int i = 0; i < size; i++ )
{
AccordionItem accordionItem = new AccordionItem( );
accordionItem.Header = "Item " + count.ToString( );
count++;
_Accordion.Items.Add( accordionItem );
Grid aGrid = new Grid( );
TextBlock tb = new TextBlock( );
tb.Text = accordionItem.Header as string;
aGrid.Children.Add( tb );
accordionItem.Content = aGrid;
//accordionItem.IsEnabled = true;
accordionItem.IsSelected = true;
}
}
private void CreateAccordionItems( object sender, RoutedEventArgs e )
{
Accordion accordion = new Accordion( );
accordion.HorizontalContentAlignment = HorizontalAlignment.Stretch;
TheStackPanel.Children.Add( accordion );
AddToAccordion( 10, accordion );
}
}
I need to use an Accordion to display some totals on a LOB application we are building.
If I place the Accordion in XAML all works fine and the state of the icon (>) is correct and pointing to the right. On Mouse entering the AccordionItem we do not have a visual state change.
If I dynamically add AccordionItems on a Button Click (to simulate async data call returning) the state of the icon is not the same and on MouseEnter it "corrects" itself by executing a visual state change. *You may need to click "Add 3 Accordion Items" twice.
If I dynamically add an Accordion on a Button click with AccordionItems it works fine. Below is my sample Application.
So what do I need to do to get the Accordion to add AcordionItems at runtime and be in the correct state as per when using XAML?
XAML
<Grid x:Name="LayoutRoot" Background="Black" >
<StackPanel x:Name="TheStackPanel">
<Button Content="Create Accordion" Click="CreateAccordionItems"></Button>
<Button Content="Add 3 Accordion Items" Click="AddAccordionItems"></Button>
<Grid Background="Pink">
<layoutToolkit:Accordion SelectionMode="ZeroOrMore" x:Name="TestAccordion" Margin="10,10,10,10" HorizontalAlignment="Stretch" >
<layoutToolkit:AccordionItem Content="Content - 1" Header="Header - 1">
</layoutToolkit:AccordionItem>
<layoutToolkit:AccordionItem Content="Content - 2" Header="Header - 2">
</layoutToolkit:AccordionItem>
<layoutToolkit:AccordionItem Content="Content - 3" Header="Header - 3">
</layoutToolkit:AccordionItem>
</layoutToolkit:Accordion>
</Grid>
</StackPanel>
public partial class MainPage : UserControl
{
private int count = 0;
public MainPage()
{
// Required to initialize variables
InitializeComponent();
//TestAccordion.ExpandDirection = ExpandDirection.Down;
}
private void AddAccordionItems( object sender, RoutedEventArgs e )
{
AddToAccordion( 3, TestAccordion );
}
private void AddToAccordion( int size, Accordion _Accordion )
{
for( int i = 0; i < size; i++ )
{
AccordionItem accordionItem = new AccordionItem( );
accordionItem.Header = "Item " + count.ToString( );
count++;
_Accordion.Items.Add( accordionItem );
Grid aGrid = new Grid( );
TextBlock tb = new TextBlock( );
tb.Text = accordionItem.Header as string;
aGrid.Children.Add( tb );
accordionItem.Content = aGrid;
//accordionItem.IsEnabled = true;
accordionItem.IsSelected = true;
}
}
private void CreateAccordionItems( object sender, RoutedEventArgs e )
{
Accordion accordion = new Accordion( );
accordion.HorizontalContentAlignment = HorizontalAlignment.Stretch;
TheStackPanel.Children.Add( accordion );
AddToAccordion( 10, accordion );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您查看 Accordian 控件的源代码,您会发现它使用 InteractionHelper.UpdateVisualState 在事件发生后设置其正确状态。
由于该方法在 Accordian 控件上标记为内部,并且 InteractionHelper 是私有变量,因此最好的选择是找出要添加控件的状态,然后告诉控件转到该状态(无需转换) ),然后将其添加到可视化树中。这就是为什么 MouseOver 会“修复”它。
If you take a look at the source code for the Accordian control you'll see that it uses the InteractionHelper.UpdateVisualState to set its correct state after events.
Since the method is marked internal on the Accordian control and the InteractionHelper is a private variable, your best bet is to figure out which of the states you're adding the control in and then tell the control to go to that state (without a transition) before addig it to the visual tree. This is why the MouseOver is "fixing" it.
称呼
TestAccordion.UpdateLayout();
添加项目后...可能是
Call
TestAccordion.UpdateLayout();
after adding the items... may be
您可以将手风琴项目绑定到 ObservableCollection 吗?
Can you bind the accordian Items to an ObservableCollection?