2013-09-30 44 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。 –

相關問題