Margin setting, width associated with ActualWidth on controls causes VS2012 to crash

I fiddled with the DataGrid headers and found something awkward. I played with him until I found the root cause in XAML and created a small sample of how to reproduce.

I tested this only with a text box and datagrid, but I suspect that it works with other controls as well. Setting the Margin property (I set Margin="2" ) either in the datagrid or in the text field, when their width is attached to the sibling element via ActualWidth , will cause all my Visual Studio to stop responding almost immediately.

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

This is a preview of the design before it stops responding. The window is stretched to "infinity" until I have no doubt that VS will run out of memory.

enter image description here

Any idea what could be causing this?

+4
source share
2 answers

Try setting HorizontalAlignment="Left" (no matter what alignment) for the StackPanel :

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

With a panel width of 180 , and all other controls, this value inherits from the binding ( DataGrid , TextBox ).

Quote from StackPanel MSDN :

The default value is stretched for both horizontal alignment and vertical selection of content contained in the StackPanel.

Since the default alignment is the NaN panel, the Width and Height panel settings are inherited from the parent, the StackPanel , which also has no explicit parameters. And in this panel, the width and height are inherited from Window .

+1
source

In WPF, widths and margins should not be specified together. Both can be used to set the size of an item. Fields do this with respect to their alignment positions.

I suspect that it happens that the margin conflicts with the specification of the width and causes some kind of exception that needs to be thrown when rendering the project. I ran into a similar problem with some DevExpress network controls that were left behind due to an actual code error in their library, but the results were very similar to what you described.

I would suggest converting your code to use only margins and alignments. See this link for more information:

http://wpftutorial.net/LayoutProperties.html

0
source

Source: https://habr.com/ru/post/1496953/


All Articles