【问题标题】:Align two pieces of text separably in a WPF button在 WPF 按钮中分别对齐两段文本
【发布时间】:2011-10-23 13:09:38
【问题描述】:

我有一个相当简单的按钮 ControlTemplate 来设置按钮的样式。 我被要求将按钮的部分文本对齐到按钮的右侧。因此,一些文本将左对齐和右对齐。 对此方法的指针将不胜感激。 这是我目前所拥有的:

<ControlTemplate x:Key="ListItemNameTemplate" TargetType="Button">
        <Grid Height="40">           

            <Border Width="200" 
                    x:Name="BgEnabled"                                                  
                    HorizontalAlignment="Center" 
                    Margin="1,1,1,1"
                    Background="YellowGreen">   

                <StackPanel Orientation="Vertical">
                    <TextBlock Width="150"
                               x:Name="textBlock" 
                               Text="{TemplateBinding Content}" 
                               HorizontalAlignment="Left" 
                               VerticalAlignment="Center" 
                               Foreground="White" 
                               FontSize="24"
                               FontWeight="Bold"/>

                    <TextBlock Width="50"                             
                               x:Name="textBlock1" 
                               Text="{TemplateBinding Content}" 
                               HorizontalAlignment="Right" 
                               VerticalAlignment="Center" 
                               Foreground="White" 
                               FontSize="24"
                               FontWeight="Bold"/>
                </StackPanel>
            </Border>
        </Grid>
    </ControlTemplate>

然而: 一种。第二个文本块不显示。 湾。我需要为第二个文本块指定不同于“内容”的内容。

【问题讨论】:

    标签: wpf xaml layout button controltemplate


    【解决方案1】:

    你可以继承Button 来为右对齐文本引入一个新的依赖属性,但是你会被Content 属性困住(如果Content 被设置,你可以创建两个新属性并抛出异常不过)。

    要获得正确的布局,您应该使用Dockpaneldock 左右,LastChildFill 设置为false)或Grid(三列,中间休息,左右休息是自动调整大小的)。

    【讨论】:

      【解决方案2】:

      像这样:

      <Window.Resources>
          <ControlTemplate TargetType="{x:Type Button}" 
                           x:Key="MyButtonTemplate">
              <Border>
                  <DockPanel LastChildFill="True">
                      <TextBlock Text="{TemplateBinding Content}"
                                 DockPanel.Dock="Top"/>
                      <TextBlock Text="{TemplateBinding Content}"
                                 TextAlignment="Right" />
                  </DockPanel>
              </Border>
          </ControlTemplate>
      </Window.Resources>
      
      <Button Template="{StaticResource MyButtonTemplate}">
          Hello World
      </Button>
      

      看起来像:

      如果您希望第二个文本不同,则只需创建第二个属性。我将创建一个附加的依赖属性,这样您仍然可以使用 TextBox,而无需对其进行子类化并搞砸项目中的所有 UI 元素。使用 sn -p "propa" 在 Visual Studio 中开始使用。这很容易。见这里:http://msdn.microsoft.com/en-us/library/ms749011.aspx

      像这样:

      public class MyButtonThing
      {
          public static string GetText2(DependencyObject obj)
          {
              return (string)obj.GetValue(Text2Property);
          }
          public static void SetText2(DependencyObject obj, string value)
          {
              obj.SetValue(Text2Property, value);
          }
          public static readonly DependencyProperty Text2Property =
              DependencyProperty.RegisterAttached("Text2", 
              typeof(string), typeof(System.Windows.Controls.Button));
      }
      

      还有这个:

      <Window.Resources>
          <ControlTemplate TargetType="{x:Type Button}" 
                           x:Key="MyButtonTemplate">
              <Border>
                  <DockPanel LastChildFill="True">
                      <TextBlock Text="{TemplateBinding Content}"
                                 DockPanel.Dock="Top"/>
                      <TextBlock TextAlignment="Right"  Text="{Binding 
                          RelativeSource={RelativeSource AncestorType=Button},
                          Path=(local:MyButtonThing.Text2)} " />
                  </DockPanel>
              </Border>
          </ControlTemplate>
      </Window.Resources>
      
      <Button Template="{StaticResource MyButtonTemplate}"
              local:MyButtonThing.Text2="Where's Waldo">
          Hello World
      </Button>
      

      【讨论】:

      • 我正在使用这个例子来解决这个问题。还没有为我工作,但目前我没有完全关注这个问题。只是想感谢您的回复。
      • 不确定我在这里错过了什么,但这个例子对我不起作用。我已经在一个新的解决方案中按照上面的代码进行了尝试。执行时出现以下异常:在“System.Windows.Baml2006.TypeConverterMarkupExtension”上提供值引发异常。内部异常是{“属性路径无效。‘MyButtonThing’没有名为‘Text2’的公共属性。”}
      【解决方案3】:

      附加的属性路由当然是解决这个问题的方法。对我来说是一个新主题,这些例子对我不起作用。控件模板无法识别新属性。 起作用的更改是修改附加属性的定义方式。

      感谢这篇文章的指点: http://www.deepcode.co.uk/2008/08/exposing-new-properties-for-control_15.html

      public class MyButtonThing : DependencyObject
      {
        public static readonly DependencyProperty SubTextProperty =
          DependencyProperty.RegisterAttached("SubText",
            typeof(String), typeof(MyButtonThing),
            new FrameworkPropertyMetadata(null,
              FrameworkPropertyMetadataOptions.AffectsMeasure |
              FrameworkPropertyMetadataOptions.AffectsArrange));
      
        public static void SetSubText(UIElement element, object o)
        {
          element.SetValue(SubTextProperty, o);
        }
      
        public static string GetSubText(UIElement element)
        {
          return (string)element.GetValue(SubTextProperty);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-12
        • 2020-08-23
        • 1970-01-01
        • 2019-10-09
        • 1970-01-01
        • 2021-12-26
        • 2017-03-17
        相关资源
        最近更新 更多