首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >How to UpdateLayout of StackPanel?

How to UpdateLayout of StackPanel?
EN

Stack Overflow用户
提问于 2011-09-03 05:59:47
回答 2查看 6.4K关注 0票数 4

问题是,如果你点击按钮并展开电话号码,堆叠面板和边框就会展开,这很好,但如果你折叠它,堆叠面板和边框就不会折叠。

代码语言:javascript
复制
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Background="White"
        >
    <StackPanel>
        <Border BorderBrush="Black" BorderThickness="1">
            <ListBox x:Name="myListBox">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Background="LightBlue" >
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="John Smith"/>
                                <Button Click="Button_Click" Width="25" Height="25"/>
                            </StackPanel>
                            <StackPanel x:Name="PhoneNumber" Visibility="Collapsed">
                                <TextBlock Text="12345"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Border>
    </StackPanel>
</Window>

使用以下代码隐藏:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            myListBox.ItemsSource = new List<int>() { 1, 2 }; //add 2 elements; 
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            StackPanel sp1 = VisualTreeHelper.GetParent(btn) as StackPanel;
            StackPanel sp2 = VisualTreeHelper.GetParent(sp1) as StackPanel;

            StackPanel phone = sp2.FindName("PhoneNumber") as StackPanel;

            if (phone.Visibility == System.Windows.Visibility.Collapsed)
                phone.Visibility = System.Windows.Visibility.Visible; 
            else
                phone.Visibility = System.Windows.Visibility.Collapsed;

            myListBox.UpdateLayout(); //these don't collapse my space
            this.UpdateLayout(); //these don't collapse my space
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-03 06:37:50

我没有深入研究这一点,但看起来你必须一直调用InvalidateMeasure直到ItemsPresenter (实际上是ItemsPanelTemplate)。如果我能提供更好的东西,我会更新的

代码语言:javascript
复制
private void Button_Click(object sender, RoutedEventArgs e)
{
    Button btn = sender as Button;

    StackPanel sp1 = VisualTreeHelper.GetParent(btn) as StackPanel;
    StackPanel sp2 = VisualTreeHelper.GetParent(sp1) as StackPanel;

    StackPanel phone = sp2.FindName("PhoneNumber") as StackPanel;

    if (phone.Visibility == System.Windows.Visibility.Collapsed)
        phone.Visibility = System.Windows.Visibility.Visible; 
    else
        phone.Visibility = System.Windows.Visibility.Collapsed;

    DependencyObject dpObject = btn;
    while (dpObject != null)
    {
        if (dpObject is UIElement)
        {
            (dpObject as UIElement).InvalidateMeasure();
        }
        if (dpObject is ItemsPresenter)
            break;
        dpObject = VisualTreeHelper.GetParent(dpObject);
    }
}
票数 3
EN

Stack Overflow用户

发布于 2011-09-03 06:44:22

问题是列表框使用了虚拟化。如果您禁用它,那么问题就会消失,如下所示:

代码语言:javascript
复制
<ListBox x:Name="myListBox" BorderBrush="Black" BorderThickness="1">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

或者,您可以保留默认的ItemsPanel并在ListBox上设置ScrollViewer.CanContentScroll="False"。不过,两者都禁用了虚拟化。

我相信this question是有关系的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7289614

复制
相关文章

相似问题

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