Question

I was messing around with DataGrid headers and discovered something awkward. I toyed with it until I found the root cause in the XAML and built a small sample of how to reproduce.

I only tested this with textbox and datagrid, but I suspect it works with other controls as well. Setting the Margin property ( I set Margin="2") on either the datagrid or the textbox when their widths are bound to a sibling element through ActualWidth will cause my entire 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 what the design preview looks before it stops responding. The window stretches into "infinity" until what I suspect to be VS running out of memory.

enter image description here

Any idea what could be causing this?

Was it helpful?

Solution

Try set HorizontalAlignment="Left" (it does not matter what kind of alignment) for StackPanel:

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

At the panel width will be 180, and all the rest controls, this value will inherit from binding (DataGrid, TextBox).

Quote from StackPanel MSDN:

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

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

OTHER TIPS

In WPF, widths and margins should not be specified together. Both can be used to set the size of an item. Margins do it relative to their Alignment positions.

I suspect that what is going on is that the margin is conflicting with the width specification and causing some kind of exception to be thrown when the design is being rendered. I encountered a similar problem with some DevExpress grid controls awhile back, due to an actual code error in their library, but the results were very similar to what you described.

I'd suggest converting your code to use just margins and alignments. See this link for more information:

http://wpftutorial.net/LayoutProperties.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top