【问题标题】:Creating not rectangle control for input为输入创建非矩形控件
【发布时间】:2013-07-11 13:48:00
【问题描述】:

为 Windows Phone 编写应用程序,我想创建自定义控件,继承自 TextBlock。但是这个控件的形式不应该是矩形。我尝试使用 Blend 来完成这项任务,但我找不到更改控件形式的属性。

在上图中,有示意图形式的控制。我想,有可能设置控制角度的坐标,但我没有找到。谢谢。

【问题讨论】:

  • 文字会在两边吗?
  • 文本将进入边框。
  • 我明白了,但只能横向还是纵向?
  • 抱歉,只能横向。
  • 为什么不直接使用专门的控件模板创建样式来改变某个TextBox 的外观而不是继承?

标签: c# wpf windows-phone-7 controls


【解决方案1】:

这个问题可以通过多种方式来解决,我选择用Template跟他们的数字。在人物角色中将执行标准RectanglesTextBlock 的模板无法设置,所以我选择了更通用的控件 - Label

例子:

<Style TargetType="{x:Type Label}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Border Background="{TemplateBinding Background}">
                    <Grid>
                        <Rectangle Width="30" Height="70" Fill="Gainsboro" StrokeThickness="1" Margin="0,0,0,10" Panel.ZIndex="0" />
                        <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="33,0,0,25" Panel.ZIndex="1" />
                        <Rectangle MinWidth="55" Height="30" StrokeThickness="1" Fill="Gainsboro" HorizontalAlignment="Left" Margin="30,30,0,0" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在 XAML 中声明 Label

<Label Background="Transparent" Width="200" Height="90" Content="Test your label" />

Output

当然,您需要更改Template 以满足您的需要。

Note about several ways

【讨论】:

  • 谢谢! windows phone 中没有 Label 控件,而 WPF 中只有 Decorator 类,但我会重写你的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多