2011-07-21 19 views
1

サイズが最大のエッジのボタンを作成したいと思います。均一な幅と高さを持つボタンを作成するにはどうすればよいですか?

この理由は、ボタンをDataGrid列に埋め込まれた小さな情報サークルにしたいからです。

現在、私が持っている:

<DataGridTemplateColumn> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <Button Content="i" Padding="2" Margin="0" 
        VerticalAlignment="Center" 
        HorizontalAlignment="Center" 
        Width="{Binding Source=Self, Path=Height}"> 
       <Button.Template> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Grid> 
          <Ellipse Fill="#FFF4F4F5" Stroke="#FF6695EB"/> 
          <ContentPresenter Margin="0" 
               RecognizesAccessKey="False" 
               SnapsToDevicePixels="True" 
               VerticalAlignment="Center" 
               HorizontalAlignment="Center" 
               /> 
         </Grid> 
        </ControlTemplate> 
       </Button.Template> 
      </Button> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 
+0

。私が下記に投稿した解決策は、ボタンの測定を無効にするので、ハードコードされた値を必要としません。 – Rob

+0

ありがとうございましたRob、そうですね、私は何かをハードコードしたくないので、編集直後にあなたのソリューションを見ています。クラスに両方のオーバーライドを追加することに決めました。設定されています。 –

+0

Width/Heightをオプションでハードコードする機能が必要な場合は、OnPropertyChangedを使用するのではなく、MeasureOverrideにこれらの追加チェックを追加することをお勧めします。 – Rob

答えて

2

は、例えば、ボタンから派生したクラスを作成し、測定を上書き

あなたは、このような何か、それを使用することができます
public class SquareButton : Button 
{ 
    protected override Size MeasureOverride(Size constraint) 
    { 
     Size size = base.MeasureOverride(constraint); 
     size.Height = size.Width = Math.Max(size.Height, size.Width); 
     return size; 
    } 
} 

:XAMLで幅または高さのいずれかをハードコーディングしている場合は、同様にそれらの両方をハードコーディング(および任意のメソッドをオーバーライドすることを避ける)こと

<local:SquareButton 
    Content="i" Padding="2" Margin="0" 
    VerticalAlignment="Center" 
    HorizontalAlignment="Center"> 
    <local:SquareButton.Template> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Grid> 
       <Ellipse Fill="#FFF4F4F5" Stroke="#FF6695EB"/> 
       <ContentPresenter Margin="0" 
        RecognizesAccessKey="False" 
        SnapsToDevicePixels="True" 
        VerticalAlignment="Center" 
        HorizontalAlignment="Center" 
        /> 
      </Grid> 
     </ControlTemplate> 
    </local:SquareButton.Template> 
</local:SquareButton> 
1

カスタムコントロールを作る - SquareButton

public class SquareButton : Button 
{ 
    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    { 
     if (e.Property == HeightProperty || e.Property == WidthProperty) 
      this.Height = this.Width = Math.Max(this.Width, this.Height); 

     base.OnPropertyChanged(e); 
    } 
} 
+0

このDerinDavisのおかげで、この問題は少なくとも1つのサイズを指定する必要があります。 –

関連する問題