2012-02-17 10 views
7

मेरे आवेदन में, मेरे पास स्क्वायर बटन का ग्रिड है। प्रत्येक बटन के लिए टेक्स्ट सामग्री रनटाइम पर सेट की जाती है। ज्यादातर मामलों में, पाठ केवल एक वर्ण लंबा होता है, लेकिन कभी-कभी यह लंबा होता है। मुझे पूरा पाठ हमेशा दिखाई देने की आवश्यकता है, यानी बटन की सीमा के अंदर फिट करने के लिए इसे खींचें (फ़ॉन्ट आकार बदलें)। मैं यह कैसे करुं?स्क्वायर बटन में टेक्स्ट कैसे फैलाएं?

मैंने एक व्यूबॉक्स का उपयोग करने की कोशिश की, लेकिन इससे मदद नहीं मिलती है।

मेरी XAML का एक सरलीकृत संस्करण:

<Viewbox Stretch="Uniform"> 
    <Button Content="Text" 
      Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/> 
</Viewbox> 

मैं कैसे प्राप्त कर सकते हैं कि मैं क्या है (यानी वर्ग बटन + पाठ कि हमेशा में फिट बैठता है) की जरूरत है पर कोई भी विचार?

+2

किसी भी कारण एक [UniformGrid] (http://www.wpftutorials.com/2011/03/wpf-uniformgrid एचटीएमएल) काम नहीं करेगा? आपका फ़ॉन्ट गतिशील रूप से बदलने वाला नहीं है, आपको उचित संपत्ति को बाध्य करने के द्वारा उसे समायोजित करने की आवश्यकता होगी। –

+0

मैं पहले से ही बटन को पकड़ने के लिए यूनिफॉर्मग्रिड का उपयोग कर रहा हूं, लेकिन यह सिर्फ उन्हें _same size_ बनने के लिए बनाता है, _square_ नहीं। – agnes

उत्तर

6

आपका मूल सुझाव लगभग सही था, इस प्रयास करें:

<Button> 
     <Viewbox Stretch="Fill"> 
      <TextBlock Text="Test"/> 
     </Viewbox> 
    </Button> 

और एकाधिक बटन को यह लागू करने के लिए:

<Style x:Key="StretchedButtonContent" TargetType="{x:Type Button}"> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <Viewbox Stretch="Fill"> 
         <ContentPresenter Content="{TemplateBinding Content}"/> 
        </Viewbox> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

    <Button Style="{StaticResource StretchedButtonContent}" Content="Test" /> 

मेरी पहली सोचा एक RenderTransform और एक कनवर्टर का उपयोग करने के लिए किया गया था। यह एक ही परिणाम देता है, लेकिन अधिक जटिल है:

<Converters:ScaleConverter x:Key="ScaleConverter" /> 

    <Button> 
     <TextBlock Text="Test" RenderTransformOrigin="0.5,0.5"> 
      <TextBlock.RenderTransform> 
       <ScaleTransform> 
        <ScaleTransform.ScaleX> 
        <MultiBinding Converter="{StaticResource ScaleConverter}"> 
         <Binding RelativeSource="{RelativeSource AncestorType={x:Type Button}}" Path="ActualWidth" /> 
         <Binding RelativeSource="{RelativeSource AncestorType={x:Type TextBlock}}" Path="ActualWidth" /> 
        </MultiBinding> 
        </ScaleTransform.ScaleX> 
        <ScaleTransform.ScaleY> 
        <MultiBinding Converter="{StaticResource ScaleConverter}"> 
         <Binding RelativeSource="{RelativeSource AncestorType={x:Type Button}}" Path="ActualHeight" /> 
         <Binding RelativeSource="{RelativeSource AncestorType={x:Type TextBlock}}" Path="ActualHeight" /> 
        </MultiBinding> 
        </ScaleTransform.ScaleY> 
       </ScaleTransform> 
      </TextBlock.RenderTransform> 
     </TextBlock> 
    </Button 

और एक कनवर्टर

public class ScaleConverter : IMultiValueConverter 
{ 
    #region Implementation of IMultiValueConverter 

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     return ((double) values[0])/((double) values[1]); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 
+0

फिल धन्यवाद, पहला समाधान सही काम करता है! – agnes

संबंधित मुद्दे