网格扩展器标头 - 无法正确排列

时间:2012-07-20 08:11:02

标签: c# wpf

我为Expander定义了以下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>

我需要在左侧显示项目名称,在组头标题的右端显示总和。但我得到的是:

enter image description here

如何将“总计”移动到标题的右端?

3 个答案:

答案 0 :(得分:5)

我遇到了同样的问题,如果我没记错,问题是ContentPresenter的{​​{1}}不关心Expander.Header的{​​{1}}。我找到了一个很好的解决方法:

HorizontalAlignment

答案 1 :(得分:0)

试试这个

<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>

我希望这会有所帮助

答案 2 :(得分:0)

通过使用ValueConverter将Expander.Header中Grid的宽度设置为相对于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,直到获得所需的结果。