2014-07-10 34 views
0

我對創建的用戶控件有問題。該控件由搜索文本框和樹視圖組成。樹視圖爲不同的節點類型顯示不同的數據模板。所以我創建了usercontrol類型爲datatemplate的依賴項屬性,可以在使用我的控件時進行綁定。在控件內部,treeview綁定到依賴項屬性。但遺憾的是treeviewtemplate選擇器沒有被調用。在用戶控件中設置樹形視圖的ItemTemplateSelector

<UserControl x:Class="yyy.yyy.yyy.UI.UserControls.SearchableTreeView.SearchableTreeView" 
     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:behaviours="clr-namespace:yyy.yyy.yyy.UI.Behaviours;assembly=yyy" 
     mc:Ignorable="d" 
     x:Name="parent" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<DockPanel DataContext="{Binding ElementName=parent}"> 
    <TextBox DockPanel.Dock="Top" Margin="5" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"/> 
    <TreeView DockPanel.Dock="Top" Margin="5" ItemsSource="{Binding TreeViewItems}" ItemTemplateSelector="{Binding TreeViewTemplateSelector}"> 
     <TreeView.ItemContainerStyle> 
      <Style TargetType="{x:Type TreeViewItem}"> 
       <Setter Property="HorizontalContentAlignment" Value="Left" /> 
       <Setter Property="VerticalContentAlignment" Value="Center" /> 
       <Setter Property="behaviours:TreeViewItemBehaviour.IsBroughtIntoViewWhenSelected" Value="true"/> 
       <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
       <Setter Property="FontWeight" Value="Normal" /> 
       <Setter Property="Visibility" Value="Visible"/> 
       <Style.Triggers> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter Property="FontWeight" Value="Bold" /> 
        </Trigger> 
        <DataTrigger Binding="{Binding Path=IsVisible}" Value="false"> 
         <Setter Property="Visibility" Value="Collapsed"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </TreeView.ItemContainerStyle> 
    </TreeView> 
</DockPanel> 

依賴屬性後面的代碼看起來像這樣:

public partial class SearchableTreeView : UserControl 
    { 
public SearchableTreeView() 
{ 
    InitializeComponent(); 
} 

public static readonly DependencyProperty TreeViewTemplateSelectorProperty = DependencyProperty.Register(
    "TreeViewTemplateSelector", typeof (DataTemplateSelector), typeof (SearchableTreeView), new PropertyMetadata(default(DataTemplateSelector))); 

public DataTemplateSelector TreeViewTemplateSelector 
{ 
    get { return (DataTemplateSelector) GetValue(TreeViewTemplateSelectorProperty); } 
    set { SetValue(TreeViewTemplateSelectorProperty, value); } 
} 


} 

而且用戶控件在那樣的XAML中:

<searchableTreeView:SearchableTreeView TreeViewTemplateSelector="{StaticResource TreeViewFieldTemplateSelector}"/> 

凡TreeViewFieldTemplateSelector是一種類型的datatemplateselector,它已經在之前工作過了從可搜索的樹視圖中創建一個用戶控件。

有人知道我在做什麼錯嗎?或者是不可能將數據模板選擇器直接綁定到樹視圖?

感謝

曼努埃爾

回答

1

您通過使用DataTemplateSelector你的系統變得複雜。雖然它確實這些對象是爲此目的而創建的,但實現您的要求有一種更簡單的方法。基本上,如果你聲明的每個數據類型一個DataTemplate沒有指定x:Key,那麼他們將被隱式應用到正確類型的所有對象:現在

<DataTemplate DataType="{x:Type YourPrefix:YourDataType"> 
    ... 
</DataTemplate> 
<DataTemplate DataType="{x:Type YourPrefix:YourOtherDataType"> 
    ... 
</DataTemplate> 
<DataTemplate DataType="{x:Type YourPrefix:SomeOtherDataType"> 
    ... 
</DataTemplate> 

,如果你把這些數據類型的項目到一個集合和數據綁定到一個集合控件,然後你會看到你的各種項目按預期呈現,但沒有DataTemplateSelector的複雜性。


UPDATE >>>

好吧,那就試試這個,而不是...首先刪除DataContext="{Binding ElementName=parent}"設置,然後添加一個RelativeSource BindingTreeViewTemplateSelector屬性:

<DockPanel> 
    <TextBox DockPanel.Dock="Top" Margin="5" Text="{Binding SearchText, 
     UpdateSourceTrigger=PropertyChanged}"/> 
    <TreeView DockPanel.Dock="Top" Margin="5" ItemsSource="{Binding TreeViewItems}" 
     ItemTemplateSelector="{Binding TreeViewTemplateSelector, RelativeSource={ 
     RelativeSource AncestorType={x:Type YourPrefix:SearchableTreeView}}}"> 
     <TreeView.ItemContainerStyle> 
      ... 
     </TreeView.ItemContainerStyle> 
    </TreeView> 
</DockPanel> 
+0

我知道,這將更容易,但問題是,我必須選擇不涉及不同類型的對象的DataTemplate。對象內部有一個屬性,DataTemplateSelector使用它來告訴wpf應該使用哪個模板。 – Maniga

+0

Thx!就是這樣。幾分鐘前,我發現這個解決方案在那裏http://www.jayway.com/2011/05/17/bind-from-xaml-to-property-defined-in-code-behind/ 問題是我有將樹視圖綁定到我的代碼隱藏文件內部的依賴項屬性,但數據上下文是控件的視圖模型。 – Maniga