2013-09-30 40 views
0

首先,让我告诉你我的代码是如何被切割的。在ItemsControl中设置deledate ResourceDictionary

我有一个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>... 

我EventsListTemplate.xaml看起来是这样的:

<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> 

而我的长相EventsListTemplate.xaml.cs代码背后像这样

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 }); 
     } 
    } 

当我在我的模板中点击我的listview项目时,它会触发事件sList_MouseDoubleCkick,我可以检索我的事件。

但是,我想在创建模板的UC中触发一个动作(第一个源代码块)。

要做到这一点,我只想创建一个委托在我的模板(技术,在一个完美的世界,像“this.rd_eventslisttemplate.MainWindowControlPointer = ...”)。但是,我不知道该怎么做,或者甚至有可能。

+0

你有一个模板。在你的模板里面你有一个Button。当你点击你的按钮时,你可以调用一个使用你的模板的控件内部的方法。基本上你想调用ItemsControl中的一个方法。我帮你解决了吗? –

+0

现在,当我点击按钮时,它会调用模板内部的方法。我想要做的是调用一个实际在使用该模板的控件内部的方法。 – Olivier

+0

使用该模板的控件是ItemsControl的权利?那么你想调用的方法的名称是什么? –

回答

1

加密你的问题一段时间后,我想我明白了。

有一个很好的方法称为VisualTreeHelper.GetParent(),它可以让你在VisualTree中的控件的父母。

现在,当您在EventsListTemplate中捕获事件时,您需要调用GetParent()几次,直到最终获得UserControl的实例。

就是这样。

+0

我试图使用“var parent = VisualTreeHelper.GetParent(this);”但它有一个编译错误:“System.Windows.Media.VisualTreeHelper.GetParent(System.Windows.DependencyObject)'的最佳重载方法匹配有一些无效参数”,“无法从'QuimeO.UserControls.Lists.EventsListTemplate 'to'System.Windows.DependencyObject'“ – Olivier

+0

好吧,明白了:VisualTreeHelper.GetParent(发件人为DependencyObject);作品。谢谢,我现在将尝试让我的用户控制:) – Olivier

+0

是的,你明白了。只允许DependencyObjects。 –

相关问题