[英]Grid expander header - can't get arrangement right
I have the following Expander
defined for a DataGrid
. 我为
DataGrid
定义了以下Expander
。
<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 item name in the left and the sum in the right end of the group header. 我需要在组标题的左侧显示项目名称,在右侧显示总和。 However what I get is this:
但是我得到的是:
How can I move the 'Total' to the right end of the header? 如何将“总计”移到标题的右端?
I had the same problem and if I remember it right the problem is that the ContentPresenter
for Expander.Header
doesn't care the HorizontalAlignment
of Expander
. 我有同样的问题,如果我没记错的话,问题是
ContentPresenter
for Expander.Header
不在乎Expander
的HorizontalAlignment
。 I found somewhere this nice workaround: 我在某个地方找到了一个不错的解决方法:
<Expander.Header>
<Grid HorizontalAlignment="{Binding Path=HorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}, Mode=OneWayToSource}">
...
</Grid>
</Expander.Header>
Try this 尝试这个
<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 will help 我希望这个能帮上忙
You can achieve the desired result by using a ValueConverter to set the width of the Grid in the Expander.Header to a value relative to the Expander.ActualWidth, like this: 您可以通过使用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>
Code for the ValueConverter: 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;
}
}
Try changing the ConverterParameter until you get the desired result. 尝试更改ConverterParameter,直到获得所需的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.