3

我想让扩展器标题中的一些文本左对齐,然后一些文本右对齐。我已经找到了如何将标题扩展到容器的宽度,并认为我可以简单地添加一个停靠面板并将第二个文本块设置为 Dock Right,但这似乎没有帮助。有什么解决办法吗?

<Expander>
  <Expander.Header>
    <DockPanel
      Width="{Binding
        RelativeSource={RelativeSource
        Mode=FindAncestor,
        AncestorType={x:Type Expander}},
        Path=ActualWidth}">
      <TextBlock
        Text="I am header text…"
        Background="LightBlue"
      />
      <TextBlock DockPanel.Dock="Right"
        Text="I am header text…"
        Background="Yellow"
      />
    </DockPanel>
  </Expander.Header>
</Expander>
4

4 回答 4

5

这对我有用。这也将箭头向右移动,因为它正在恢复标题的顺序并重新恢复内容的顺序。

<Expander FlowDirection="RightToLeft" >  
  <Expander.Header>
    <DockPanel FlowDirection="LeftToRight" 
          HorizontalAlignment="{Binding HorizontalAlignment,
                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}},
                          Mode=OneWayToSource}"
          LastChildFill="True">
      <!-- Change the look inside header here -->

      <Label Content="Header" Background="Red" />
    </DockPanel>
  </Expander.Header>

  <Expander.Content>
    <!-- Change the look inside content here, by default Expander.Content is stretched -->
    <DockPanel FlowDirection="LeftToRight" LastChildFill="True">
      <Label Content="Left" Background="Aquamarine" />
      <Label Content="Fill" Background="Plum" />
    </DockPanel>
  </Expander.Content>
</Expander>

来源

于 2013-06-27T04:06:26.953 回答
1

尝试将TextAlignment属性设置为 right 并将属性设置为在sHorizontalAlignment上伸展。TextBlock这应该有助于我想。

如果您使用颜色不是为了演示目的,并且您确实希望整个元素右对齐,那么您可能需要考虑将LastChildFill属性设置DockPanel为 false。

于 2009-04-08T09:30:08.433 回答
1

不幸的是,这与默认扩展器模板的问题有关,该模板将标题的水平对齐设置为向左而不是拉伸。让它工作的最好方法是创建一个正确设置它的新模板。以下是更多信息的链接:

http://silverlight.net/forums/p/57142/145801.aspx#145801

它适用于 Silverlight,但也适用于 wpf。另一种方法是将上面的停靠面板宽度绑定到包含扩展器的元素的实际宽度。这不是一个很好的解决方案,但它有效。您需要为宽度创建一个值转换器。这是一些代码:

[ValueConversion(typeof(double), typeof(double))]
public class OffsetDoubleConverter : IValueConverter
{
    #region IValueConverter Members

    public double Offset { get; set; }
    public bool KeepPositive { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double number = (double)value + Offset;
        if ((KeepPositive) && (number < 0.0))
        {
            number = 0.0;
        }
        return number;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double number = (double)value - Offset;
        if ((KeepPositive) && (number < 0.0))
        {
            number = 0.0;
        }
        return number;
    }

    #endregion
}

在你的 xaml 中:

<!-- create a ref to your namespace -->
xmlns:loc="clr-namespace:YourNamespace"

...

<Window.Resources>
<loc:OffsetDoubleConverter x:Key="ExpanderConverter" Offset="-208.0" KeepPositive="True"/>
</Window.Resources>

...

<DockPanel Width="{Binding ElementName=ADifferentElement, Path=ActualWidth,
                   Converter={StaticResource ExpanderConverter}}">
...

同样,这不是最好的解决方案,但它应该可以工作。需要注意的一点是,如果将偏移值设置得太小并将其绑定到扩展器的父级,则可以让 Visual Studio 挂起,因为父级的实际宽度将基于扩展器的宽度。

如果这个实现不是很清楚,请告诉我。同样,我真的建议只为扩展器使用自定义模板。您可以获取默认模板,只需稍加修改即可使其正常工作。如果你愿意,我也可以发布。

于 2009-04-08T15:27:21.897 回答
0

我通过在 Expander.Header 部分添加一个网格并将其宽度设置为其祖先并拉伸它来解决这个问题。现在网格按预期工作。

    <Expander HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" >
            <Expander.Header>
                <Grid HorizontalAlignment="Stretch" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=ActualWidth}">
                    <Grid.ColumnDefinitions>
                         ...
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                         ...
                    </Grid.RowDefinitions>
                    <TextBlock Text="{Binding _header}" TextAlignment="Center" HorizontalAlignment="Stretch" />
                </Grid>
            </Expander.Header>
    </Expander>
于 2022-01-19T18:52:34.863 回答