简体   繁体   English

wpf-扩展器的标题适合内容的宽度?

[英]wpf - Header of Expander fit contents width?

Is there any way to make the Header of an Expander Fit the max-width in a resizeable window in WPF? 有什么方法可以使ExpanderHeader适合WPF中可调整大小的窗口中的最大宽度吗? It appears that no matter what I do I can't get the contents to expand to maximum width. 看来,无论我做什么,我都无法使内容扩展到最大宽度。 It does work in the Content section of the Expander . 它确实在ExpanderContent部分中起作用。

在此处输入图片说明

<Grid Background="LightGray">

    <Expander IsExpanded="True">
         <Expander.Header>
              <Grid Width="100" Height="50">
                   <Rectangle Fill="Red"></Rectangle>
              </Grid>
         </Expander.Header>

         <Rectangle Fill="Red"></Rectangle>

    </Expander>

</Grid>

The fastest way is to bind the width of the header to the width of the whole expander. 最快的方法是将标头的宽度绑定到整个扩展器的宽度。

<Expander IsExpanded="True">
  <Expander.Header>
      <Grid Width="{Binding RelativeSource={RelativeSource
            Mode=FindAncestor,
            AncestorType={x:Type Expander}},
            Path=ActualWidth}"
            Height="50">
            <Rectangle Fill="Red"></Rectangle>
      </Grid>
  </Expander.Header>
  <Rectangle Fill="Red"></Rectangle>
</Expander>

But it's not a precise method because you have the arrow which also takes some space and you can see that the header is a bit wider than you need. 但这不是一种精确的方法,因为您的箭头也占用了一些空间,并且可以看到页眉比您需要的宽一些。 更改扩展器标题的宽度

You can override the standard template ( HeaderTemplate ) of the expander's header. 您可以覆盖扩展器标头的标准模板( HeaderTemplate )。

UPDATE UPDATE

I found a possible solution using the code-behind file (all credits go to @kmatyaszek). 我发现了使用代码隐藏文件的可行解决方案(所有资源都归@kmatyaszek所用)。

Add a helper class to find the control we need to change the width. 添加一个帮助器类以查找我们需要更改宽度的控件。 It checks the whole Visual Tree of the parent control and returns a child of the type we are looking for. 它检查父控件的整个可视树,并返回我们要查找的类型的子级。

public static class VTHelper
{
    public static T FindChild<T>(DependencyObject parent) where T : DependencyObject
    {
        if (parent == null) return null;

        T childElement = null; 
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent); 
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            T childType = child as T; 
            if (childType == null)
            {
                childElement = FindChild<T>(child); 
                if (childElement != null) 
                    break;
            }
            else
            {
                childElement = (T)child; 
                break;
            }
        } 
        return childElement;
    }
}

Add a handler to process the loading event. 添加一个处理程序以处理加载事件。 It changes the HorizontalAlignment property of the ContentPresenter instance: 它更改ContentPresenter实例的HorizontalAlignment属性:

private void expander_Loaded(object sender, RoutedEventArgs e)
{
    var tmp = VTHelper.FindChild<ContentPresenter>(sender as Expander);
    if (tmp != null)
    {
        tmp.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    }
}

Attach this handler to the expander: 将此处理程序附加到扩展器:

<Expander IsExpanded="True" Loaded="expander_Loaded">

This approach uses the code-behind but it doesn't work with any data (or ViewModel). 这种方法使用了后台代码,但不适用于任何数据(或ViewModel)。 It changes only the visual appearance of the control. 它仅更改控件的视觉外观。

        <Expander Name="myexpander" IsExpanded="True" Margin="0,0,1,0">
            <Expander.Header>
                <Grid Width="{Binding ElementName=myexpander, Path=ActualWidth}" Height="50">
                    <Rectangle Fill="Red"></Rectangle>
                </Grid>
            </Expander.Header>

            <Rectangle Fill="Red"></Rectangle>

        </Expander>

    </Grid>

give a name to your expander and bind the width property 给扩展器起个名字并绑定width属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM