gpt4 book ai didi

c# - 如何更新 StackPanel 的布局?

转载 作者:太空狗 更新时间:2023-10-30 01:25:16 25 4
gpt4 key购买 nike

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

<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>

使用以下代码隐藏:

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
}
}
}

最佳答案

没有深入研究这个问题,但看起来您必须一直调用 InvalidateMeasure 直到 ItemsPresenter(实际上,项目面板模板)。如果我能有更好的东西,我会更新

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);
}
}

关于c# - 如何更新 StackPanel 的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7289614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com