-1

In a xaml DataTemplate I have

<DockPanel >
    <StackPanel DockPanel.Dock="Right" Orientation="Vertical">
        <Button />
        <Button />
    </StackPanel>
    <Grid VerticalAlignment="Stretch/Center">
        <TextBlock HorizontalAlignment="Stretch"
                   TextAlignment="Right"
                   Text="{Binding ...}"
                   VerticalAlignment="Stretch"
                   DockPanel.Dock="Right"/>
    </Grid>
</DockPanel>

The dockpanel is horizontal, i.e. stackpanel is to the right, the grid to the left.

I need the two buttons to stretch each to 50% of the vertical space of height of the dockpanel. How do I do it?

A schematic picture of want I try to achieve. The controls are meant to stretch to the borders as much as possible. enter image description here

I need the textblock to stretch to 100% of the vertical space of height of the dockpanel and it's text to be vertically centered. It doesn't work bc. in the case of Strech I get the 100% space but the text is vertically topped and in the case of Center the textblock is not stretched.

Gerard
  • 13,023
  • 14
  • 72
  • 125
  • I am not following. Do you want the `TextBlock` on top of the buttons? Please post a picture of what you want. – mm8 Dec 10 '20 at 20:35
  • The dockpanel is horizontal, i.e. stackpanel is to the right, the grid to the left. – Gerard Dec 10 '20 at 20:38
  • 1
    Where should the text be displayed? A `TextBlock` cannot both "stretch to 100% of the vertical space" and also be vertically centered obviously. – mm8 Dec 10 '20 at 20:41
  • Why is this obvious I don't get that? Do you mean that a TextBlock is always constricted to its text in size? – Gerard Dec 10 '20 at 20:48
  • Why do you want the `TextBlock` to stretch? The TextBlock is just text and you also say that you want the text in the middle...? Again, please post a picture of what you actually want. It's still unclear. – mm8 Dec 10 '20 at 20:49
  • It's not my evening - the picture in Word gives me all kinds of troubles. You are right that it is silly to stretch the TextBlock. The Real Reason is, that I am trying to exactly mimic the DataGridTextColumn, I want my DataGridTemplateColumn to be/behave exactly the same. I shall probably have to abondon that idea. – Gerard Dec 10 '20 at 20:55
  • How is a `DataGridTextColumn` related to this question? – mm8 Dec 10 '20 at 21:03
  • I try to mimic a DataGridTextColumn via a DataGridTemplateColumn. I use TextBlock in the DataTemplate of it's CellTemplate (as above) and TextBox in the DataTemplate of it's CellEditingTemplate. It works but now I am trying to get details like going into edit mode after pressing a key and the alignment (question above) equal to the DataGridTextColumn. – Gerard Dec 10 '20 at 21:08
  • Sorry, I cannot understand how a `DockPanel` with two buttons relates to a `DataGridTextColumn`. – mm8 Dec 10 '20 at 21:11
  • I added the picture. This is the content of one DataGridCell of the DataGridTemplateColumn – Gerard Dec 10 '20 at 21:22

1 Answers1

1

A TextBlock cannot both "stretch to 100% of the vertical space" and also be vertically centered. It's just text after all.

You should be able to get rid of the StackPanel and replace the DockPanel with a 2x2 Grid. Something like this:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*" />
        <ColumnDefinition Width="1*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button Grid.Column="1" Content="Button1" />
    <Button Grid.Column="1" Content="Button2" Grid.Row="1" />
    <Grid Grid.RowSpan="2">
        <TextBlock Grid.RowSpan="2"
                   TextAlignment="Right"
                   Text="Text..."
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"/>
    </Grid>
</Grid>

enter image description here

mm8
  • 163,881
  • 10
  • 57
  • 88