This project is read-only.

how to set event only on specific vertex?

Feb 1, 2013 at 1:16 PM
Edited Feb 1, 2013 at 1:21 PM
hello everyone,
I'm using <DataTemplate.Triggers> to display different pictures in different kinds of vertex, however I couldn't find a way to set only on specific type of vertex that display specific pictures mouse click event. I need only specific vertex to handle the event. can anyone help me?

<DataTemplate x:Key="DataTemplateVertexControl"
                      DataType="local:GraphVertex">
            <StackPanel>
                   <!-- Display the title of the vertex -->
                   <TextBlock Text="{Binding Path=Title, Mode=OneWay}" FontSize="12"/>
                   <Image x:Name="img" Width="50" Height="50" />                
            </StackPanel>            
            <DataTemplate.Triggers> 
                <DataTrigger Binding="{Binding Name}" Value="root">
                    <Setter TargetName="img" Property="Source" Value="Images/picServer.png"/>              
                    <Setter TargetName="img" Property="Width"  Value="60" />
                    <Setter TargetName="img" Property="Height"  Value="60" />                     
                </DataTrigger>
                <DataTrigger Binding="{Binding Name}" Value="vertex_kind_A">
                    <Setter TargetName="img" Property="Source"      Value="Images/pic123.png"/>
                    <Setter TargetName="img" Property="Width"  Value="60" />
                    <Setter TargetName="img" Property="Height"  Value="60" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <!-- Customize the graph vertex -->
        <Style TargetType="{x:Type graphsharp:VertexControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <!-- Bind the DataTemplate -->
                    <ControlTemplate TargetType="{x:Type graphsharp:VertexControl}">
                            <ContentPresenter Content="{TemplateBinding Vertex}"
                                              ContentTemplate="{StaticResource DataTemplateVertexControl}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style> 
I need that only when the image "img" is "pic123" mouse click event will be handled
Feb 1, 2013 at 4:45 PM
Edited Feb 1, 2013 at 5:00 PM
Maybe you could have a trigger with an EventSetter in the VertexControl style? I haven't tried placing an EventSetter in a trigger but it should work:
<!-- Customize the graph vertex -->
        <Style TargetType="{x:Type graphsharp:VertexControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <!-- Bind the DataTemplate -->
                    <ControlTemplate TargetType="{x:Type graphsharp:VertexControl}">
                            <ContentPresenter Content="{TemplateBinding Vertex}"
                                              ContentTemplate="{StaticResource DataTemplateVertexControl}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                 <DataTrigger Binding="{Binding Name}" Value="vertex_kind_A">
                      <EventSetter Event="MouseLeftButtonDown" Handler="OnVertexKindA_MouseClick" />
                 </DataTrigger>
            <style.Triggers>
        </Style> 
This should make clicks on vertices whose "Name" is "vertex_kind_A" be handled by "OnVertexKindA_MouseClick".
Or you could also have the handler work on all vertices and inside of it check what kind of vertex was clicked and act accordingly.

EDIT: Nevermind, sorry, EventSetters are NOT supported inside Triggers. You could still still use a single handler for all vertices and check the "Name" property of the clicked vertex.
Feb 2, 2013 at 11:57 AM
I would rather not handle event if no need in it in all vertices.
Anyway, I can't set handler on the vertices, only on the image itself and then check the source name of the image int the event,
It's not a good solution, since that way I can't change the picture. Can you tell me where to define the handler of the event to get the vertex name in the function?
thank you for your help
Feb 4, 2013 at 10:01 AM
If you define it in the VertexControl style with an EventSetter, the handler will work for all VertexControls and in it you can use the 'sender' argument to see which vertex was clicked:
<Style TargetType="{x:Type graphsharp:VertexControl}">
     <EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnVertex_PreviewMouseLeftButtonDown" />
     .........
</ Style>
private void OnVertex_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    VertexControl clicked = sender as VertexControl;
    if(clicked != null)
    {
         if((clicked.Vertex as YourVertexType).Name == "vertex_kind_A")
         {
            //handle the click on vertex_kind_A
          }
     }
}