1

I am trying to set the alignment of the contents in the ListBox below. I have wrapped the ListBox inside a ScrollViewer to make it scroll horizontally.

Each element of the Listbox is a Stackpanel and I am trying to align each one to the Top and to set the width to auto for each.

Right now, the stackpanels are centered vertically and the width of each one is the same and is represented by the width of the largest panel.

I have put HorizontalContentAlignment="Left" and VerticalContentAlignment="Top", as I have read in similar posts but it does not change anything.

<ScrollViewer Grid.Row="1"   VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"  >
                <ListBox ItemsSource="{Binding Termene}" SelectedItem="{Binding SelectedTermenCondica}"  HorizontalContentAlignment="Left" VerticalContentAlignment="Top">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Rows="1"   />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate >
                        <DataTemplate >
                            <StackPanel Margin="2"  >
                                <StackPanel.Resources>
                                    <CollectionViewSource Source="{Binding Dosare}" x:Key="dosareView"  IsLiveSortingRequested="True" >
                                        <CollectionViewSource.SortDescriptions>
                                            <scm:SortDescription PropertyName="Ora" />
                                        </CollectionViewSource.SortDescriptions>
                                    </CollectionViewSource>
                                    <Style x:Key="NoFocusColumStyle" TargetType="{x:Type DataGridCell}">
                                        <Setter Property="IsHitTestVisible" Value="False"/>
                                    </Style>
                                </StackPanel.Resources>
                                <TextBlock Text="{Binding NumeComplet, StringFormat='Complet {0}'}" HorizontalAlignment="Center" FontWeight="Bold" VerticalAlignment="Top"></TextBlock>
                                <TextBlock Text="{Binding TermenCB, StringFormat='Termen: {0}'}" HorizontalAlignment="Center" FontWeight="Bold" VerticalAlignment="Top"></TextBlock>
                                <DataGrid IsSynchronizedWithCurrentItem="True" CellStyle="{StaticResource NoFocusColumStyle}" IsReadOnly="True" ItemsSource="{Binding Source={StaticResource dosareView}}" Style="{StaticResource DGRapaorte}" AutoGenerateColumns="False" >

                                    <DataGrid.Columns>
                                        <DataGridTextColumn  Header="Nr. dosar" Binding="{Binding NumarDosar}"/>
                                        <DataGridTextColumn CanUserSort="True" SortDirection="Ascending" SortMemberPath="Ora"  Header="Ora" Binding="{Binding Ora, StringFormat={}{0:HH:mm}}" />
                                        <DataGridTextColumn Header="Obiect" Binding="{Binding Obiect}"/>
                                    </DataGrid.Columns>
                                </DataGrid>
                               </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </ScrollViewer>

Later edit: I have added some data (three items in Termene to show you how it looks. I have blurred them out, but you can see the issue. Here is how it looks right now: enter image description here

Scrolled further: enter image description here

Now, here is how I would like it to look like: enter image description here

As you can see, the height and width of every item is set with the width and height of the largest item.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cosmin
  • 411
  • 4
  • 16
  • add a picture of what it looks like right now and a picture of what you want it to look like. i have problems to understand what you actually want to accomplish. – Welcor May 01 '20 at 21:07
  • @Blechdose, thank you for your input, I have edited my post, check it out. – Cosmin May 02 '20 at 08:22
  • okay, now i understand your issue. It is too much work to test around with your code, without having a minimal working example. Because I would have to create an example viewModel with dummy data for it. But here are some things that i noticed: The TextBlocks are in a vertical Stackpanel. Thus the `VerticalAlignment="Top"`attribute should have no effect at all. because the stackpanel will simply stack everything inside it on top of each other. some things you could try out: remove `UniformGrid` part. Remove `scrollviewer`, listboxes can already scroll. Remove `HorizontalContentAlignment` – Welcor May 02 '20 at 09:16
  • nevermind... i found the problem, i post an answer. – Welcor May 02 '20 at 09:30

1 Answers1

1

Replace the Uniformgrid part with this:

<ItemsPanelTemplate>
    <StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>

Source: https://stackoverflow.com/a/3565590/4394435

enter image description here

This example worked for me:

<Window x:Class="WpfMVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfMVVM"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="500">
    <ListBox ItemsSource="{Binding Items}" VerticalContentAlignment="Top">
        <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="2">
                    <TextBlock Text="test" HorizontalAlignment="Center" FontWeight="Bold" />
                    <TextBlock Text="blub" HorizontalAlignment="Center" FontWeight="Bold" />
                    <DataGrid IsSynchronizedWithCurrentItem="True" IsReadOnly="True"
                              ItemsSource="{Binding DataGridItems}" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Nr. dosar" Binding="{Binding Text}" />
                        </DataGrid.Columns>
                    </DataGrid>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>

with this viewModel

public class DataViewModel
{
    public DataViewModel()
    {

    }

    public List<Item> Items { get; set; } = new List<Item>()
    {
        new Item(){DataGridItems = new List<DataGridItem>(){new DataGridItem() { Text = "a"} }},
        new Item(){DataGridItems = new List<DataGridItem>(){new DataGridItem() { Text = "aaaaaaaaaaa"} }},
        new Item(){DataGridItems = new List<DataGridItem>(){new DataGridItem() { Text = "aaaaaaaaaaaaaaaaaa"} }},
        new Item(){DataGridItems = new List<DataGridItem>()
        {
            new DataGridItem() { Text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
            new DataGridItem() { Text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
            new DataGridItem() { Text = "aaa"},
            new DataGridItem() { Text = "aaaaaaaaaa"}
        }}
    };
}

public class Item
{
    public List<DataGridItem> DataGridItems { get; set; }
}

public class DataGridItem
{
    public string Text { get; set; }
}
Welcor
  • 2,431
  • 21
  • 32
  • Thanks! It solved the width issue. However, try and put multiple values in one of the Items and and you would notice that the Items are still centered (the second issue) and not alligned to the Top. It doesn't work if I put VerticalAlignment="Top" as a Property for the StackPanel that is set as the DataTemplate. – Cosmin May 02 '20 at 10:07
  • @MarianV. i dont understand. you mean multiple DataGridItems? they are aligned to the top. i updated the picture. Or do you mean the textboxes? they are centered because of the HorizontalAligment Attribute. or what do you mean? – Welcor May 02 '20 at 10:36
  • I was referring to the aligning to the Top of the DataGridItems. I created a new project and now it works. I have to find the culprit, it must be in the Application Resources because I am using a Style Library (ModernWpf from Kinnara) – Cosmin May 02 '20 at 12:12
  • 1
    I got it to work, I just had to set the ItemContainerStyle for the ListBox. Thank you again – Cosmin May 02 '20 at 12:49