2016-04-24 16 views
2

ボタンの(テキスト)コンテンツの自動化に問題があります。WPF ItemsControl ButtonContentのFontSize

これは私のボタンのスタイルです。特別なものはありませんが、TextWrapingのプロパティに気づくことはありません!

<Style TargetType="{x:Type Button}"> 
    <Setter Property="Template"> 
    <Setter.Value> 
    <ControlTemplate TargetType="Button"> 
     <TextBlock Text="{TemplateBinding Content}" TextWrapping="Wrap"/> 
    </Setter.Value> 
    </Setter> 
</Style> 

私は私のボタンに基づいてItemTemplateに定義:

<DataTemplate x:Key="MyItemTemplate"> 
    <Button Content="{Binding Content}" Command="{Binding Command}" FontWeight="Bold" FontSize="{Binding FontSize}"/> 
</DataTemplate> 

を私は3列でのItemsControlと中項目のリストを表示しています:この時点までは

<ItemsControl ItemsSource="{Binding MyItems}" ItemTemplate="{StaticResource MyItemTemplate}"> 
    <ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <UniformGrid Columns="3"/> 
    </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 

、この何も特別なものではない。しかし今、私はすべての私のボタンのFontSizeを最大に伸ばしたい、最大のコンテンツを持つアイテムが許す(すべてのアイテムは最後に同じFontSizeを持つべきです)。

テキストの必要なサイズを計算するためにsolutionが見つかりました。だから私は、ボタンの最大フォントサイズを計算することができます。しかし、私は私のボタンのいずれかの実際のサイズが必要です(すべて同じサイズです)。

サイズを取得して問題を解決する方法は実際にはわかりません。誰かがアイデアを持っていれば素晴らしいでしょう。まったく違うアプローチです。

答えて

0

次のような幅を取得するためにCommandParameterとしてあなたのボタンのActualWidthを使用することができます。

<UserControl .... 
.................> 
<i:Interaction.Triggers> 
     <i:EventTrigger EventName="Loaded"> 
      <i:InvokeCommandAction Command="{Binding Command}" CommandParameter="{Binding ActualWidth,ElementName=button}"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 

    <DataTemplate x:Key="MyItemTemplate"> 
        <Button Content="{Binding Content}" x:Name="button" 
        Command="{Binding Command}" 
        FontWeight="Bold" FontSize="{Binding FontSize}"/> 
       </DataTemplate> 

あなたのコマンドは、このようにする必要があります:私は、任意のコマンドを使用する前に、私は幅を必要とする

 public ICommand Command => new DelegateCommand<object>(ExecuteCommand); 

     private void ExecuteCommand(object obj) 
     { 
      double width = (double)obj; 
     } 
+1

。 – Fruchtzwerg

+0

私はUserControlの読み込まれたイベントの値を取得するように私の答えを編集しました。 – Johannes

関連する問題