2017-10-12 14 views
0

어떻게 된 ControlTemplate에 다각형을 추가 할 수 있습니다ControlTemplate to button 어떻게 내가이 작업을 수행 할 수 있지만 코드 :된 ControlTemplate WPF

<Button x:Name="btnNew" Content="click me" HorizontalAlignment="Left" Margin="54,10,0,0" VerticalAlignment="Top" Width="250" Height="250" BorderThickness="0" UseLayoutRounding="True" > 


     <Button.Template> 
      <ControlTemplate> 
       <Polygon x:Name="poly" Points="0,50 200,20 230,150 20,200"> 
        <Polygon.Fill> 
         <ImageBrush x:Name="imgtem" Stretch="Fill" ImageSource="Images/67.png"> 
         </ImageBrush> 
        </Polygon.Fill> 
       </Polygon> 
      </ControlTemplate> 
     </Button.Template> 
    </Button> 
+0

의 가능한 복제 https://stackoverflow.com/questions/732736/define-a-wpf-controltemplate-at-runtime) – ASh

답변

1

당신은의 ControlTemplate 다음 간단하게 설정 한 속성을 만들 수 XamlReader.Parse을 사용할 수 있습니다 Button :

string xaml = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><Polygon x:Name=\"poly\" Points=\"0,50 200,20 230,150 20,200\"><Polygon.Fill><ImageBrush x:Name=\"imgtem\" Stretch=\"Fill\" ImageSource=\"Images/67.png\"></ImageBrush></Polygon.Fill></Polygon></ControlTemplate>"; 
ControlTemplate template = System.Windows.Markup.XamlReader.Parse(xaml) as ControlTemplate; 

Button button = new Button(); 
button.Name = "btnNew"; 
button.Content = "Click me"; 
button.HorizontalAlignment = HorizontalAlignment.Left; 
button.Margin = new Thickness(54, 10, 0, 0); 
//... 
button.Template = template; 
[런타임에서 정의 된 ControlTemplate WPF (