【问题标题】:change Expander header ContentPresenter HorisontalAlign property更改 Expander 标头 ContentPresenter HorisontalAlign 属性
【发布时间】:2012-07-18 22:08:35
【问题描述】:

我想在 Expander 控件中有一个自定义标题。我想要一个左对齐的标题文本和一个右对齐的图片:

<Expander>
    <Expander.Header>
        <DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
            <TextBlock DockPanel.Dock="Left" Text="Some Header Text"/>
            <Image DockPanel.Dock="Right" HorizontalAlignment="Right"  />
         </DockPanel>
     </Expander.Header>
     <StackPanel >
         <ItemsPresenter />
     </StackPanel>
</Expander>

不幸的是,标题在默认情况下不会水平拉伸的元素内呈现。该元素是一个 ContentPresenter 控件,默认情况下它不会拉伸,因为它的 HorisontalAlign 值为 Left。如果我将其更改为 Stretch(我已在 Snoop 工具中完成此操作),则标题会按照我的意愿呈现。但是如何从代码中更改它?

我尝试使用正确的 Horizo​​ntalAlignment 值将 ContentPresenter 样式添加到 Expander 资源,但不幸的是它不起作用。可能 ContentPresenter 应用了一些自定义样式,这就是它没有抓住我的样式的原因。我试过这样:

<Expander.Resources>
    <Converters:TodoTypeToHeaderTextConverter x:Key="TodoTypeToHeaderTextConverter" />
    <Style TargetType="ContentPresenter">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
    </Style>
</Expander.Resources>

那么我还能尝试什么?

【问题讨论】:

    标签: wpf header styling expander alignment


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      试试这样的:

      XAML 文件:

      <Expander Name="exp" Header="test" Loaded="exp_Loaded">
                  <Expander.HeaderTemplate>
                      <DataTemplate>
                          <DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
                              <TextBlock DockPanel.Dock="Left" Text="{Binding}"/>
                              <Image Source="/ExpanderStyle;component/animation.png" Width="20"
                                     DockPanel.Dock="Right" HorizontalAlignment="Right" />
                          </DockPanel>
                      </DataTemplate>
                  </Expander.HeaderTemplate>                
              </Expander>
      

      代码隐藏:

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

      还有助手类:

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

      【讨论】:

      • 这段代码看起来很有希望,但 FindChild 只为我返回 null。尽管我能够使用 Visual Studio 检查器在 Visual Tree 中看到 ContentPresenter。
      • VisualTreeHelper.GetChildrenCount(expander) 为我返回 0。
      猜你喜欢
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      相关资源
      最近更新 更多