2014-12-26 29 views
1

在我的應用程序中,我有一個特定大小的按鈕,我不想更改。開始時該按鈕的內容爲「0」,並且每當有人按下按鈕時,按鈕增加1,例如, 3次按後內容將爲「3」。在Windows Phone的按鈕內重新調整文本內容

但是經過這麼長時間後,內容變得太大(比如說在100)以適應按鈕,並且它的結尾將完全消失在按鈕的邊緣。我希望能夠做的是讓文本重新調整大小,並在需要時縮小文字大小,以便它們全部保留在按鈕內,但會保持最大大小。

我正在尋找類似於那些計算器的東西,隨着數字的增加,它們變得更小,因此整個數字可以放在裏面。

但是我不想改變按鈕的尺寸,他們需要保持完全一樣。我將內容適配到按鈕的大小,而不是將按鈕的大小調整爲內容。

我願意接受任何解決方案或解決方法。

回答

1

您必須覆蓋Style中的按鈕Template並將換成ViewBox

enter image description here

這裏是例子(尋找Viewbox added評論):

 <Style x:Key="ButtonWithViewbox" TargetType="Button"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Grid x:Name="Grid" Background="Transparent"> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualStateGroup.Transitions> 
             <VisualTransition From="Pressed" To="PointerOver"> 
              <Storyboard> 
               <PointerUpThemeAnimation Storyboard.TargetName="Grid"/> 
              </Storyboard> 
             </VisualTransition> 
             <VisualTransition From="PointerOver" To="Normal"> 
              <Storyboard> 
               <PointerUpThemeAnimation Storyboard.TargetName="Grid"/> 
              </Storyboard> 
             </VisualTransition> 
             <VisualTransition From="Pressed" To="Normal"> 
              <Storyboard> 
               <PointerUpThemeAnimation Storyboard.TargetName="Grid"/> 
              </Storyboard> 
             </VisualTransition> 
            </VisualStateGroup.Transitions> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="PointerOver"/> 
            <VisualState x:Name="Pressed"> 
             <Storyboard> 
              <PointerDownThemeAnimation Storyboard.TargetName="Grid"/> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedForegroundThemeBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedBackgroundThemeBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Disabled"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledForegroundThemeBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Border"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledBorderThemeBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledBackgroundThemeBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Border x:Name="Border" CornerRadius="5" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" Background="{TemplateBinding Background}"> 
           <Viewbox> /* VIEWBOX ADDED */ 
            <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" 
                  ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" 
                  Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                  Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
           </Viewbox> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
0

使用Viewbox控件控制,視框被設計爲填充可用空間,無論大小都可以調整按鈕內容。

1日法

<Button Content="Long Text Long Text Long Text Long Text Long Text Long Text" Foreground="Red" Height="30"> 
     <Button.ContentTemplate> 
      <DataTemplate> 
       <Viewbox > 
        <TextBlock Margin="5,0,5,0" Text="{Binding Path=Content,RelativeSource={RelativeSource AncestorType={x:Type Button}}}" ></TextBlock> 
       </Viewbox> 
      </DataTemplate> 
     </Button.ContentTemplate> 
    </Button> 

注:你可以存儲這個DataTemplate中的資源也使用X:鍵,然後分配給按鈕<Button ContentTemplate={StaticResource DatatemplateKeyname}/>


第2種方法

<Button Height="30" Width="200" Foreground="Red"> 
     <Button.Content> 
      <Viewbox> 
       <TextBlock Margin="5,0,5,0" Text="Long Text Long Text Long Text Long Text Long Text Long Text" ></TextBlock> 
      </Viewbox> 
     </Button.Content> 
    </Button> 

注: 您可以使用標籤屬性這裏存儲的內容,然後綁定在視框中到文本塊TextBlock Text="{Binding Path=Tag,RelativeSource={RelativeSource AncestorType={x:Type Button}}}"


3方法

<!--Here Width is store in Button "Tag" property.--> 
    <Viewbox Height="30" Width="{Binding ElementName=Buttonname,Path=Tag}"> 
     <Button x:Name="Buttonname" Content="Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text" Tag="200" Foreground="Red"/> 
    </Viewbox> 
相關問題