2011-07-17 24 views
5
<DrawingImage x:Key="HexagonImage"> 
    <DrawingImage.Drawing> 
     <DrawingGroup> 
      <GeometryDrawing Brush="White" 
         Geometry="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z"> 
       <GeometryDrawing.Pen> 
        <Pen Brush="Black" Thickness="10" LineJoin="Round"/> 
       </GeometryDrawing.Pen> 
      </GeometryDrawing> 
     </DrawingGroup> 
    </DrawingImage.Drawing> 
</DrawingImage> 

<Style x:Key="HexagonButton" TargetType="{x:Type Button}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Grid> 
       <Image x:Name="hexImg" Source="{StaticResource HexagonImage}"/> 
       <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      </Grid> 
     </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

, bir denetimin rengini değiştirmek ve ben programlı, Iv'e backgroup özelliğini değiştirerek çalıştı rengini değiştirmek istiyorum ama boşuna.WPF: programlı onun tarzı olarak bu HexagonButton sahip özel bir stil bir düğme var

Bunu yapmayı başardığım tek yol, yeni bir Çizim resmiyle yepyeni bir stil oluşturmaktır. ve atayın. Ama emin olmanın daha kolay bir yolu var.

cevap

2

ben Button şablonunda doğrudan GeomteryDrawing dahil ve (hangi ı stil bildiriminde varsayılan atanan) onun Button atasının Foreground ve Background özellikleri RelativeSource ciltlerini kullanarak gibi işe aldım:

<Style x:Key="HexagonButton" TargetType="{x:Type Button}"> 
    <Setter Property="Background" Value="White" /> 
    <Setter Property="Foreground" Value="Black" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Grid> 
        <Image x:Name="hexImg"> 
         <Image.Source> 
          <DrawingImage> 
           <DrawingImage.Drawing> 
            <DrawingGroup> 
             <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=Background}" Geometry="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z"> 
              <GeometryDrawing.Pen> 
               <Pen Brush="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=Foreground}" Thickness="10" LineJoin="Round" /> 
              </GeometryDrawing.Pen> 
             </GeometryDrawing> 
            </DrawingGroup> 
           </DrawingImage.Drawing> 
          </DrawingImage> 
         </Image.Source> 
        </Image> 
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Button Style="{StaticResource HexagonButton}">Click me</Button> 

ve özel bir düğme:

varsayılan beyaz ve siyah düğme daha sonra

<Button Style="{StaticResource HexagonButton}" Background="Yellow" Foreground="Red">Click me</Button> 
+1

Küçük not: Lütfen "RelativeSource AncestorType = {x: Type Button}" yerine "RelativeSource TemplatedParent" (veya "TemplateBinding") kullanın. –

1

Arka planı yalnızca altıgen etrafında değiştirmek isterseniz, ControlTemplate ürününüzün Grid ürününe Background="{TemplateBinding Background}" ekleyin.

Ayrıca, altıgen iç kısmının arka plan rengini değiştirmek isterseniz, GeometryDrawing ürününüzün Transparent ürününü Brush değiştirin.

İlgili konular