gpt4 book ai didi

.net - 在控件上设置边距、宽度绑定(bind)到实际宽度会导致 VS2012 崩溃

转载 作者:行者123 更新时间:2023-12-01 05:18:30 25 4
gpt4 key购买 nike

我在玩弄 DataGrid头,发现了一些尴尬的事情。我一直在玩弄它,直到在 XAML 中找到根本原因并构建了一个如何重现的小样本。

我只使用文本框和数据网格对此进行了测试,但我怀疑它也适用于其他控件。设置Margin属性(我设置 Margin="2" )在数据网格或文本框上,当它们的宽度通过 ActualWidth 绑定(bind)到同级元素时将导致我的整个 Visual Studio 几乎立即停止响应。

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="WidthAndHeight">

<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" x:Name="panel">
<TextBlock Text="Text:1" Width="90" />
<TextBox Width="90" />
</StackPanel>
<DataGrid AutoGenerateColumns="False" Width="{Binding ActualWidth, ElementName=panel}">
<DataGrid.Columns>
<DataGridTextColumn Header="Test" Width="*" />
<DataGridTextColumn Header="Test2" Width="*" />
<DataGridTextColumn Header="Test3" Width="*" />
</DataGrid.Columns>
</DataGrid>
<TextBox Width="{Binding ActualWidth, ElementName=panel}" />
</StackPanel>
</Window>

这是设计预览在停止响应之前的样子。窗口延伸到“无穷大”,直到我怀疑 VS 内存不足。

enter image description here

知道是什么原因造成的吗?

最佳答案

尝试设置HorizontalAlignment="Left" (无论哪种对齐方式)对于StackPanel :

<StackPanel x:Name="panel" Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock Text="Text:1" Width="90" />
<TextBox Width="90" />
</StackPanel>

面板宽度为 180 ,以及所有其他控件,此值将从绑定(bind)( DataGridTextBox )继承。

引自 StackPanel MSDN :

The default value is stretch for both HorizontalAlignment and VerticalAlignment of content that is contained in a StackPanel.



由于默认对齐 NaN面板,那个面板设置 WidthHeight继承自父级 - StackPanel , 谁也没有明确的参数。在这个面板中,宽度和高度继承自 Window .

关于.net - 在控件上设置边距、宽度绑定(bind)到实际宽度会导致 VS2012 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18223226/

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