0

I am new to wpf

<Window.Resources>
        <Ellipse x:Key="connectorNode" Height="20" Width="20" Fill="Green" Stroke="Black" StrokeThickness="2" MouseMove="Ellipse_MouseMove" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" MouseLeftButtonUp="Ellipse_MouseLeftButtonUp"></Ellipse>
 </Window.Resources>

How can i add an instance of the Ellipse in the resources to a canvas, i only want to specify Canvas.Left and Canvas.Right but use the same property values as in resources

<Canvas>
</Canvas>
user1492051
  • 886
  • 8
  • 22

2 Answers2

2

It seems like you want to create a generic style and apply it to every Ellipse added, this is how you can do it:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="EllipseStyle" TargetType="Ellipse">
            <Setter Property="Height" Value="20"/>
            <Setter Property="Width" Value="20" />
            <EventSetter Event="Control.MouseMove" Handler="Control_MouseMove" />
        </Style>
    </Window.Resources>
    <Grid>
        <Canvas>
            <Ellipse Style="{StaticResource EllipseStyle}" />
        </Canvas>
    </Grid>
</Window>

Edit: I've added "EventSetter" so your events can be defined in the style (see this post).

Community
  • 1
  • 1
Terry
  • 5,132
  • 4
  • 33
  • 66
1

If you really want to use an element from resources and apply only canvas properties you can do it this way:

<Window.Resources>
    <ControlTemplate x:Key="connectorNode" >
        <Ellipse 
            Height="20" 
            Width="20" 
            Fill="Green" 
            Stroke="Black" 
            StrokeThickness="2" 
            MouseMove="Ellipse_MouseMove" 
            MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" 
            MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" />
    </ControlTemplate>
</Window.Resources>
<Canvas>
    <ContentControl Template="{StaticResource connectorNode}"/>
    <ContentControl Canvas.Left="50" Template="{StaticResource connectorNode}"/>
</Canvas>
Novitchi S
  • 3,711
  • 1
  • 31
  • 44