开发者

Inheriting values declared at UserControl instantiation

First off, I apologize if this question has been asked before. I've done a bit of Google searching but I'm not really sure what the correct keywords are to find what I'm looking for.

Basically my problem is simple to understand. I have a Silverlight project and on the MainPage.xaml I have declared a UserControl and given it a height and a width.

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

Now within AlarmButton I have a button that has its own Control Template which is set up the way I want. It has a content presenter within it right now.

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

Later on I'm going to bind the Text property to a Depende开发者_开发百科ncyProperty so I can use this button with multiple text. What I want to happen is if the text is too big it will ellipse it and not have the box change or the textblock overflow. I just need to bind the height and width of the TextBlock to some values to contain it.

My question is this; is it possible for the TextBlock to bind its height and width to the values as declared in the MainPage.xaml? Or is this more complicated than I imagine? Is there a better way to go about this?

EDIT

This might give a little more info on what I'm trying to accomplish. This is my "button" with RobSiklos' changes

Inheriting values declared at UserControl instantiation


I think the problem has something to do with the Viewbox, which is telling the TextBlock that it has as much room as it wants.

Probably removing the Viewbox will solve the issue (or at least take the ContentPresenter out of the Viewbox)


I'm not sure I completely understand. Are you trying to make the text ellipse because you don't want the text block to grow if the text is too big? If so, you could use a converter (in the binding statement of the Text property) to have the the converter return the full text value if the text is under a certain length, or have the converter show an ellipse if the text is over that value.


This ought to work:-

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

It assumes that a Width and Height will be specified. An alternative would be to be to use the SizeChanged event of the UserControl to assign values of Width and Height directly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜