右对齐 wpf 扩展器标题中的项目

发布于 2024-07-16 08:59:38 字数 640 浏览 14 评论 0原文

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

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

I want to have some text in an expander header left aligned, then some text right aligned.
I have found how to expand the header to the width of the container, and thought I could simply add a dockpanel and set the second text block to Dock Right, but it doesn't seem to help. Any solutions?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何必那么矫情 2024-07-23 08:59:38

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

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

(来源)

This is what worked for me. This also moves the arrow to the right, because it's reverting the order of the header and re-reverting the order for the content.

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

(Source)

圈圈圆圆圈圈 2024-07-23 08:59:38

尝试将 TextAlignment 属性设置为 right,将 Horizo​​ntalAlignment 属性设置为在 TextBlock 上拉伸。 我想这应该有帮助。

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

Try setting the TextAlignment property to right and the HorizontalAlignment property to stretch on the TextBlocks. That should help i think.

If your use of color is for something other than demo purposes and you literally want the whole element to be right aligned then you might want to look at setting the LastChildFill property of the DockPanel to false.

淡淡の花香 2024-07-23 08:59:38

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

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 可能会挂起,因为父级的实际宽度将基于扩展器的宽度。

如果这个实现还不太清楚,请告诉我。 再次,我真的建议只使用扩展器的自定义模板。 您可以获得默认模板,只需稍加修改即可使其正常工作。 如果您愿意,我也可以发布该内容。

Unfortunately, this has to do with an issue with the default expander template, which sets the horizontal alignment of the header to left instead of stretch. The best way to get it working would be to create a new template that sets this correctly. Here's a link for more info:

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

it's for silverlight, but applies to wpf as well. Another way to do this would be to bind your dockpanel width above to the actual width of the element containing the expander. This isn't a great solution but it works. You'll need to create a value converter for the width. Here's some code:

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

And in your 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}}">
...

Again, this isn't the best solution, but it should work. One thing to note, if you set the offset value too small and bind it to a parent of your expander, you can get visual studio to hang since the actualwidth of the parent would be based on your width of the expander.

Let me know if this implementation isn't quite clear. Again, I would really recommend just using a custom template for the expander. You can get the default template and just slightly modify it to make it work. I can post that as well if you'd like.

黑凤梨 2024-07-23 08:59:38

我通过在 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>

I solved this problem by adding a grid into the Expander.Header section and set it's width to its ancestor and stretching it. Now the Grid works as expected.

    <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文