【问题标题】:Inheriting values declared at UserControl instantiation继承在 UserControl 实例化时声明的值
【发布时间】:2011-08-11 23:53:05
【问题描述】:

首先,如果之前有人问过这个问题,我深表歉意。我在 Google 上进行了一些搜索,但我不太确定找到我要查找的内容的正确关键字是什么。

基本上我的问题很容易理解。我有一个 Silverlight 项目,在 MainPage.xaml 上我声明了一个 UserControl 并为其指定了高度和宽度。

<Grid>
        <control:AlarmButton Height="50" Width="50" />
</Grid>

现在在 AlarmButton 中我有一个按钮,它有自己的控制模板,它按照我想要的方式设置。它现在有一个内容展示器。

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="Alarms.AlarmButton">

    <UserControl.Resources>
        <ControlTemplate x:Key="StatusButton" >
            <Viewbox Stretch="Fill">
                <Grid>
                    <Path x:Name="Base" StrokeThickness="1.0" Stroke="#ff000000" StrokeMiterLimit="1.0" Fill="#ff666666" Data="F1 M 99.500,99.500 L 0.500,99.500 L 0.500,0.500 L 99.500,0.500 L 99.500,99.500 Z"/>
                    <Path x:Name="Interior" Opacity="0.5" StrokeThickness="1.0" Stroke="#ff191919" StrokeMiterLimit="1.0" Data="F1 M 97.500,97.500 L 2.500,97.500 L 2.500,2.500 L 97.500,2.500 L 97.500,97.500 Z">
                        <Path.Fill>
                            <LinearGradientBrush MappingMode="Absolute" StartPoint="2.500,2.499" EndPoint="97.500,97.499">
                                <LinearGradientBrush.GradientStops>
                                    <GradientStop Offset="0.00" Color="#3FFFFFFF"/>
                                    <GradientStop Offset="0.151" Color="Transparent"/>
                                    <GradientStop Offset="1.00" Color="#BFFFFFFF"/>
                                    <GradientStop Color="#53FFFFFF" Offset="0.655"/>
                                </LinearGradientBrush.GradientStops>
                                <LinearGradientBrush.Transform>
                                    <MatrixTransform Matrix="1.000,0.000,-0.000,-1.000,0.000,100.000" />
                                </LinearGradientBrush.Transform>
                            </LinearGradientBrush>
                        </Path.Fill>
                    </Path>
                    <Path x:Name="LargeShader" Opacity="0.1" Fill="#ffffffff" Data="F1 M 94.667,18.667 L 94.667,94.667 L 6.333,94.667 C 6.333,94.667 94.667,67.348 94.667,18.667 Z"/>
                    <Path x:Name="SmallShader" Opacity="0.1" Fill="#ffffffff" Data="F1 M 94.667,43.667 L 94.667,94.667 L 20.333,94.667 C 20.333,94.667 94.667,76.334 94.667,43.667 Z"/>
                    <ContentPresenter x:Name="contentPresenter" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" 
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                      Margin="{TemplateBinding Padding}" 
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
            </Viewbox>
        </ControlTemplate>
    </UserControl.Resources>

    <Grid >
        <Button Template="{StaticResource StatusButton}" >
            <TextBlock Text="this is a text box" 
                       TextTrimming="WordEllipsis" />
        </Button>
    </Grid>


</UserControl>

稍后我会将 Text 属性绑定到 DependencyProperty,以便我可以将此按钮与多个文本一起使用。我想要发生的是,如果文本太大,它会省略它并且不会改变框或文本块溢出。我只需要将 TextBlock 的高度和宽度绑定到一些值以包含它。

我的问题是这样的; TextBlock 是否可以将其高度和宽度绑定到 MainPage.xaml 中声明的值?还是这比我想象的要复杂?有没有更好的方法来解决这个问题?

编辑

这可能会提供有关我正在尝试完成的工作的更多信息。这是我的 RobSiklos 更改的“按钮”

【问题讨论】:

  • 您需要TextBlock 成为ViewBox 的孙子吗?短的时候需要增长吗?

标签: silverlight xaml data-binding


【解决方案1】:

我认为问题与 Viewbox 有关,它告诉 TextBlock 它有尽可能多的空间。

移除 Viewbox 可能会解决问题(或至少将 ContentPresenter 移出 Viewbox)

【讨论】:

  • 这是我正在寻找的解决方案。我试图用任何形状保留按钮的外观,这是 Viewbox 的原因。您的解决方案有点工作,但 TextBlock 从“按钮”开始,这是不受欢迎的。我想我可能只需要再搞砸一点。
  • 实际上,这正是我想要的方式......我刚刚意识到我的 ViewBox 没有拉伸。我不知道,我的解决方案可能不是完成所有这些的最佳方式,但我在 XAML 方面并没有真正的经验
【解决方案2】:

我不确定我是否完全理解。您是否试图使文本椭圆,因为如果文本太大,您不希望文本块增长?如果是这样,您可以使用转换器(在 Text 属性的绑定语句中)让转换器在文本低于特定长度时返回全文值,或者如果文本超过该长度,则让转换器显示椭圆价值。

【讨论】:

  • 我添加了一张图片,为您提供更多关于正在发生的事情的信息。
【解决方案3】:

这应该可以工作:-

<Grid x:Name="LayoutGrid">
    <Button Template="{StaticResource StatusButton}" >
        <TextBlock Text="this is a text box" TextTrimming="WordEllipsis"
            Width="{Binding Parent.Width, ElementName=LayoutRoot}"
            Height="{Binding Parent.Height, ElementName=LayoutRoot}" />
    </Button>
</Grid>

假定将指定宽度和高度。另一种方法是使用 UserControl 的 SizeChanged 事件直接分配 Width 和 Height 的值。

【讨论】:

  • 我认为这会很好,但由于某种原因我无法让它工作。
猜你喜欢
  • 1970-01-01
  • 2019-02-07
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 2019-07-16
  • 2015-07-27
  • 1970-01-01
相关资源
最近更新 更多