Question

First, let me show you how my code is cut.

I have this code in a xaml UC (eventsUC.xaml) :

<UserControl x:Class="QuimeO.UserControls.Lists.EventsListUC"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:ToggleSwitch="clr-namespace:ToggleSwitch;assembly=ToggleSwitch" 
         mc:Ignorable="d" 
         Width="477"
         >
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <ScrollViewer HorizontalScrollBarVisibility="Disabled" Width="auto" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <ItemsControl Grid.Row="0" BorderThickness="0" x:Name="eventsList" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ItemsControl.Resources>
                <ResourceDictionary x:Name="eventslisttempplate" Source="EventsListTemplate.xaml"  />
            </ItemsControl.Resources>
        </ItemsControl>
    </ScrollViewer>...

My EventsListTemplate.xaml looks like this :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    x:Class="QuimeO.UserControls.Lists.EventsListTemplate"
                    xmlns:apiNamespace="clr-namespace:QuimeO.DBO"
                    >
    <DataTemplate DataType="{x:Type apiNamespace:EventsDictionary}">
        <StackPanel Orientation="Vertical">
            <Border BorderBrush="LightGray" BorderThickness="0,0,0,1" Margin="0,5,0,0">
                <TextBlock HorizontalAlignment="Right" Foreground="Gray" Text="{Binding FormatedDate}" FontSize="14"></TextBlock>
            </Border>
            <ListView Grid.Row="0" x:Name="eventsList" ItemsSource="{Binding Events}" BorderThickness="0" MouseDoubleClick="eventsList_MouseDoubleClick">
            </ListView>
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>

And my EventsListTemplate.xaml.cs code behind looks like this

public partial class EventsListTemplate : ResourceDictionary
    {
        public Delegate MainWindowControlPointer;

        private void eventsList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var ev = ((sender as ListView).SelectedItem as ListViewItem).DataContext as Event;
            System.Diagnostics.Debug.WriteLine(ev.Name);

            this.MainWindowControlPointer.DynamicInvoke(ev.Ancestors.Root, new Element() { Category = ev.Category, Id = ev.Id, Name = ev.Name });
        }
    }

When I click on an item of my listview in my template, it trigger the eventsList_MouseDoubleCkick and I can retrieve my event.

However, I would like to trigger an action in the UC where the template is created (first source code block).

To do that, I just want to create a delegate in my Template (technicaly, in a perfect world, something like "this.rd_eventslisttemplate.MainWindowControlPointer = ... "). But, I don't know how to do it or if this is even possible.

Was it helpful?

Solution

After encrypting your question for a while I think I got it.

There is a nice method called VisualTreeHelper.GetParent() which gets you the parent of a control in VisualTree.

Now to your problem when you catch your event in EventsListTemplate you will need to call GetParent() few times till you finally get the instance of your UserControl.

Thats it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top