开发者

C# WPF Problem with building Shapes

开发者 https://www.devze.com 2023-03-29 10:59 出处:网络
i have created a shape which lies in canvas Element: <Canvas HorizontalAlignment=\"Left\" Width=\"47\" Height=\"71\">

i have created a shape which lies in canvas Element:

<Canvas HorizontalAlignment="Left" Width="47" Height="71">
  <Polygon Points="25 0 10 43 40 43" Stroke="Black" Fill="Orange" Height="45"   Canvas.Left="0" Canvas.B开发者_JAVA百科ottom="0" Width="47"></Polygon>
            <Ellipse Height="20" Width="20" Stroke="Black" Fill="Black" Canvas.Bottom="45" Canvas.Left="15"></Ellipse>
     </Canvas>

And i when i click on Button it should create such an element in another Canvas Container!

How could i reuse this constructed Shape several time in my Code without to write it more than one time in XAML?

I have read something about Templates and Styles. But i could not really adept it to my problem.Because i want to create this shapes at runtime.


you can declare the Polygon in resources like this

<Window.Resources>
        <Polygon x:Key = "Poly" Points="25 0 10 43 40 43" Stroke="Black" Fill="Orange" Height="45"   Canvas.Left="0" Canvas.Bottom="0" Width="47"/>
</Window.Resources>

Then you can use it like this

   <ContentControl Content="{StaticResource Poly}"/>

Similarly you can do

<Window.Resources>
    <Canvas HorizontalAlignment="Left" Width="47" Height="71" x:Key="MyShape">
        <Polygon Points="25 0 10 43 40 43" Stroke="Black" Fill="Orange" Height="45"   Canvas.Left="0" Canvas.Bottom="0" Width="47"></Polygon>
        <Ellipse Height="20" Width="20" Stroke="Black" Fill="Black" Canvas.Bottom="45" Canvas.Left="15"></Ellipse>
    </Canvas>
</Window.Resources>

<ContentControl Content="{StaticResource MyShape}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消