4

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
jamesSampica
  • 12,230
  • 3
  • 63
  • 85
  • Try `HorizontalAlignment="Left"` for each control (`StackPanel`, `DataGrid`, `TextBox`). At the panel width will be `180`, and all the rest, this value will inherit. I think it's a feature of `StackPanel`. – Anatoliy Nikolaev Aug 14 '13 at 04:56

2 Answers2

1

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.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
  • 1
    Yes, I believe the issue is the default `HorizontalAlignment="Stretch"` for the stackpanel. When the control gets a `Margin="2"` the `ActualWidth` of the sibling element is increased, causing the control's width to increase and we end up in an infinite loop of both control's trying to satisfy their widths. – jamesSampica Aug 14 '13 at 14:25
0

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

Joe Brunscheon
  • 1,949
  • 20
  • 21
  • _widths and margins should not be specified together._ I don't believe this is true. Margin specifies space outside of ActualWidth, so the two properties should operate ok together – jamesSampica Aug 14 '13 at 13:48
  • *Should* operate together. However, in my experience, you will spend more time fighting with the controls and sizes, margins, and alignments. Learning to use sizes or margins, then finding what works best for you will yield better results and fewer headaches in the long run. – Joe Brunscheon Aug 14 '13 at 13:53