Grid Expander Header - Cannot Be Configured Correctly

I have the following Expanderone specific for DataGrid.

<Expander IsExpanded="True" HorizontalAlignment="Stretch" Background="Blue">
    <Expander.Header>
            <Grid HorizontalAlignment="Stretch" Background="BurlyWood">
                      <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="*" />
                      </Grid.ColumnDefinitions>

                      <TextBlock Text="{Binding Path=Name, StringFormat=\{0:D\}}" FontWeight="Bold" />
                      <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1">
                        <TextBlock Text="Total : "/>
                        <TextBlock Text="{Binding Path=Items, Converter={StaticResource sumConverter}}" FontWeight="Bold"/>
                      </StackPanel>
                </Grid>
        </Expander.Header>
        <ItemsPresenter />
</Expander>

I need to display the element name on the left and the sum at the right end of the group header. However, I get the following:

enter image description here

How to move "Total" to the right end of the header?

+5
source share
3 answers

I had the same problem, and if I remember correctly, the problem is that ContentPresenterfor Expander.Headerdoes not care HorizontalAlignmentof Expander. I found this good workaround somewhere:

<Expander.Header>
    <Grid HorizontalAlignment="{Binding Path=HorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}, Mode=OneWayToSource}">
        ...
    </Grid>
</Expander.Header>
+5
source

try it

<Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                      <ColumnDefinition Width="5" /> //space between two textblocks
                    <ColumnDefinition Width="Auto" />
                  </Grid.ColumnDefinitions>

                  <TextBlock Text="{Binding Path=Name, StringFormat=\{0:D\}}" FontWeight="Bold" />
                    <TextBlock Text="Total : " Grid.Column="2"/>
                    <TextBlock Text="{Binding Path=Items, Converter={StaticResource sumConverter}}" FontWeight="Bold"  Grid.Column="4"/>

            </Grid>

I hope this helps

0
source

, ValueConverter, Expander.Header Expander.ActualWidth, :

    <Expander Name="MyExpander" IsExpanded="True" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Background="Blue">
        <Expander.Header>
            <Grid HorizontalAlignment="Stretch" Background="BurlyWood" Width="{Binding ElementName=MyExpander, Path=ActualWidth, Converter={MyConverters:ExpanderHeaderWidthConverter}, ConverterParameter=30}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding Path=Name, StringFormat=\{0:D\}}" FontWeight="Bold" />
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="2">
                    <TextBlock Text="Total : "/>
                    <TextBlock Text="{Binding Path=Items, Converter={StaticResource sumConverter}}" FontWeight="Bold"/>
                </StackPanel>
            </Grid>
        </Expander.Header>
        <ItemsPresenter />
    </Expander>

ValueConverter:

public abstract class BaseConverter : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

public class ExpanderHeaderWidthConverter : BaseConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // The actuacl Width of the Expander
        double width = (double)value;

        // Default width difference
        double diff = 25.0;

        if (parameter != null)
        {
            // If the parameter is not null, try to use it as width difference
            Double.TryParse(parameter.ToString(), out diff);
        }

        // If width - diff is less than 0, return double.NaN instead.
        if (width - diff < 0)
        {
            return double.NaN;
        }

        // Return the modified width
        return width - diff;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double width = (double)value;
        double diff = 25.0;
        if (parameter != null)
        {
            Double.TryParse(parameter.ToString(), out diff);
        }
        return width + diff;
    }
}

ConverterParameter, .

0

All Articles