HorizontalAlignment on DataTemplate in GridView (bug?)

We have a ListView with a GridView, witch has a DataTemplate on the columns. The DataTemplate consist of a border with a TextBox.
In May CTP the HorizontalAlignment=Stretch on the Border and TextBox worked fine, but this doesn’t seem to be the case in June CTP

Best regards,
  Thomas Andersen

 



Answer this question

HorizontalAlignment on DataTemplate in GridView (bug?)

  • Pawel J

    Hehe, thanks. Didn't think of that dirty workaround.

    I would however classify it as a bug. It worked fine in May CTP (I haven't tested July CTP though).
    I'll use the workaround for the time being, thanks.

    Best regards,
    Thomas Andersen


  • 93_confirmed

    If you can post a simple repro of the problem I can help you diagnose.

  • pattyg

    change the datatemplate to include Width binding

    <DataTemplate x:Key="dataTemplate">

    <TextBox Background="Gray" Width="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=ColumnDefinitions[0].ActualWidth}" Text="{Binding Mode=OneTime}" />

    </DataTemplate>



  • hcaihao

    Aha, I got the right solution from Microsoft:

    <snipped from Connect>
    This change is a result of a bug fix where ListViewItem was not respecting the default value of its HorizontalContentAlignment property. The default property value is Left so your TextBoxes should have appeared left aligned in the columns but because of that bug, ListView was stretching them.

    To get stretching behavior, set HorizontalContentAlignment="Stretch" on your ListViewItems or add a style for ListViewItem that sets this property.

    <ListViewItem Content="Test 1" HorizontalContentAlignment="Stretch"/>

    </snipped from Connect>

    In other words:

    1. Set HorizontalAlignment to Stretch on the DataTemplate

    2. Add this style to your resources:
    <Style TargetType='{x:Type ListViewItem}'>
    <Setter Property='HorizontalContentAlignment' Value='Stretch'/>
    </Style>

    Best regards,
    Thomas Andersen


  • Nelson Chang

    I have tried to make a simple reproduction of the problem. It's not exactly the way we do it in the application, but I think it's the same problem that occurs.

    Note that the TextBox doesn't stretch.

    <Window x:Class="XAMLSandbox.Window4"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    Title="XAMLSandbox" Height="300" Width="300"
    >
    <
    Window.Resources>
    <
    DataTemplate x:Key="dataTemplate">
    <
    TextBox HorizontalAlignment="Stretch" Text="{Binding Mode=OneTime}" />
    </
    DataTemplate>
    </
    Window.Resources>
    <
    Grid>
    <
    ListView>
    <
    ListView.View>
    <
    GridView>
    <
    GridViewColumn Header="Col1" CellTemplate="{StaticResource dataTemplate}" Width="100" />
    <
    GridViewColumn Header="Col2" CellTemplate="{StaticResource dataTemplate}" Width="100" />
    </
    GridView>
    </
    ListView.View>
    <
    ListViewItem Content="Test 1" />
    <
    ListViewItem Content="Test 2" />
    <
    ListViewItem Content="Test 3" />
    </
    ListView>
    </
    Grid>
    </
    Window>

    Hope for a solution as we use it in an editable grid.

    Best regards,
    Thomas Andersen


  • Focus

    Is this a bug or how can I stretch

     


  • HorizontalAlignment on DataTemplate in GridView (bug?)