Pawan
Pawan

Reputation: 1654

how to get the element's Property to which a DataTemplate is applied

i am applying different DataTemplates to elements(which are actually events with datetime and some other properties) of same type depending on a property of the element.

the element is actually telerik's RadTimeline.

the DataTemplates are as follows

    <DataTemplate x:Key="eventXTemplate">
            <Border Width="10" Height="10" Margin="0,0,0,5">
                <Rectangle Height="7"
                Width="7"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Fill="X" // different fill colors depending on event type
                Cursor="Hand"             
                MouseLeftButtonDown="Event_Clicked">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="45">
                        </RotateTransform>
                    </Rectangle.RenderTransform>
                </Rectangle>
            </Border>
        </DataTemplate>

now in the want to access the datetime property of the element in Event_Clicked method. how do it access it because the sender i get is of type rectangle.

--edit-

    <local:CustomItemTemplateSelector x:Key="ItemTemplateSelector"
                                    EventATemplate="{StaticResource eventATemplate}"
                                    EventBTemplate="{StaticResource eventBTemplate}"/>

    namespace timelineControl
    {
     public class CustomItemTemplateSelector : DataTemplateSelector
    {
    public DataTemplate EventATemplate { get; set; }
    public DataTemplate EventBTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        TimelineDataItem data = item as TimelineDataItem;
        items timelineItem = new items();

        if (data == null)
            timelineItem = item as items;
        else
            timelineItem = data.DataItem as items;

        if (timelineItem == null)
            return base.SelectTemplate(item, container);

        switch (timelineItem.Eventtype)
        {
            case "1": return this.EventATemplate;
            default: return this.EventBTemplate;
        }
    }
   }

Upvotes: 1

Views: 191

Answers (2)

Maximus
Maximus

Reputation: 3448

You can use interaction/interactivity from Blend. In order to do this Tools -> Library Package Manager -> Console Package Manager and paste Install-Package System.Windows.Interactivity.WPF then add namespaces

xmlns:ic="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

and in Rectangle you can do as follows

   <Rectangle ...> 
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonDown">
                <i:InvokeCommandAction Command="{Binding Event_Clicked}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=RadTimeline}, Path=SomeKindOfProperty}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Rectangle>

Upvotes: 0

har07
har07

Reputation: 89325

You may want to try to get it from Rectangle's DataContext :

var dc = ((Rectangle)sender).DataContext;

Upvotes: 1

Related Questions