2015-09-30 48 views
0

自定義控件已經自定義控件的內容定義如下:創建在WPF

<DockPanel> 
<ListView x:Name="TabControlMenu" DockPanel.Dock="Top" ScrollViewer.VerticalScrollBarVisibility="Disabled" 
      ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=Items}"> 
    <ListView.Resources> 
     <Style BasedOn="{StaticResource ListViewStyle}" TargetType="{x:Type ListView}" /> 
     <Style BasedOn="{StaticResource ListViewItemStyle}" TargetType="{x:Type ListViewItem}" /> 
    </ListView.Resources>    
    <ListView.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal"></StackPanel> 
     </ItemsPanelTemplate> 
    </ListView.ItemsPanel> 
</ListView> 
<Border BorderBrush="Black" BorderThickness="1,0,0,0" DockPanel.Dock="Bottom"> 
    <ContentPresenter DataContext="{Binding SelectedItem}" Content="{Binding Body, ElementName=ThisControl}" /> 
</Border> 

public partial class MyTabControl : UserControl { 

    #region static properties 

    public static readonly DependencyProperty BodyProperty = DependencyProperty.Register(
     "Body", 
     typeof(object), 
     typeof(MyTabControl), 
     new UIPropertyMetadata(null)); 

    public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
     "Items", 
     typeof(ObservableCollection<object>), 
     typeof(MyTabControl), 
     new UIPropertyMetadata(null)); 

    public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
     "SelectedItem", 
     typeof(object), 
     typeof(MyTabControl), 
     new UIPropertyMetadata(null)); 

    #endregion 

    #region public properties 

    public object Body { 
     get { return GetValue(BodyProperty); } 
     set { SetValue(BodyProperty, value); } 
    } 

    public ObservableCollection<object> Items { 
     get { return (ObservableCollection<object>)GetValue(ItemsProperty); } 
     set { SetValue(ItemsProperty, value); } 
    } 

    public object SelectedItem { 
     get { return GetValue(SelectedItemProperty); } 
     set { SetValue(SelectedItemProperty, value); } 
    } 

    public ObservableCollection<ListViewItem> ListViewItems { get; set; } 

    #endregion 

    public MyTabControl() { 
     InitializeComponent(); 

     ListViewItems = new ObservableCollection<ListViewItem>(); 
    } 

} 

,並用它是這樣的:

<Controls:MyTabControl Margin="5,0,0,0" Items="{Binding Items}"> 
    <Controls:MyTabControl.Body> 
     <ContentControl Content=" WHAT BINDING GOES HERE???? "> 
      <!--<ContentControl Content="{Binding}">--> 
      <ContentControl.Resources> 
       <DataTemplate DataType="{x:Type ViewModels:ProjectViewModel}"> 
        <Partials:ProjectView DataContext="{Binding Path=.}" /> 
       </DataTemplate> 
       <DataTemplate DataType="{x:Type ViewModels:TestSuiteViewModel}"> 
        <Partials:TestSuiteView DataContext="{Binding Path=.}" /> 
       </DataTemplate> 
       <DataTemplate DataType="{x:Type ViewModels:StepViewModel}"> 
        <Partials:StepView DataContext="{Binding Path=.}" /> 
       </DataTemplate> 
      </ContentControl.Resources> 
     </ContentControl> 
    </Controls:MyTabControl.Body> 
</Controls:MyTabControl> 

我怎樣才能建立ContentControlContent綁定,以便它指向中定義的內容210?

也許有些人會問爲什麼我創建擺在首位的自定義選項卡控制 - 答案是:因爲我必須做一些不規範 - 分組標籤,着色等周圍拖動......

編輯:

我在這裏的目標是MyTabControl表現得像一個標準的WPF TabControl - 所以它的ItemsItemsSource應綁定到某個標籤的一些收集和內容應該綁定到任何當前選擇。爲了實現這一點,我在我的控制代碼後面綁定了ListViewItems集合,並試圖將ContentPresenterDataContext綁定到當前選定的任何內容(它也在代碼後面設置)。問題是,如果我在應用程序的某個地方使用MyTabControl.Body,我不知道如何綁定ContentControl.Content屬性,以便它可以從我的自定義控件中獲取它的數據。

回答

0

如果我理解正確,您需要將UserControl設爲DataContext。問題是,您希望UserControl內的控件將UserControl視爲DataContext,但您需要UserControl本身才能看到其他一些Datacontext。您可以通過設置根佈局元素的DataContext來實現這一點,我認爲這是DockPanel。像這樣:

<UserControl 
    ... 
    x:Name="ThisControl 
    ... > 

    <DockPanel DataContext="{Binding ElementName=ThisControl}"> 

     ... 
    </ListView> 
    ... 
    </DockPanel> 
</UserControl> 

http://www.codeproject.com/Articles/325911/A-Simple-Pattern-for-Creating-Re-useable-UserContr是我想說的東西的正確解釋。

編輯: 我想可能是這樣的,你需要,以獲得DataContextContentPresenter

<DockPanel DataContext="{Binding ElementName=ThisControl}"> 
... 
    <ListView ItemsSource="{Binding Items}" ... /> 

    <Border ... > 
    <ContentPresenter DataContext="{Binding SelectedItem}" ... /> 
    </Border> 

或者你有RelativeSource做了ContentPresenter,你已經爲ItemsSourceListView

<DockPanel> 
<ListView ... 
    ItemsSource="{Binding RelativeSource= 
    {RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
    Path=Items}"> 
    ... 
</ListView> 

<Border ...> 
    <ContentPresenter DataContext="{Binding RelativeSource= 
    {RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
    Path=DataContext.SelectedItem}" 
    ... /> 
</Border> 

2日編輯:

看起來好像您正試圖在XAML級別設置MyTabControl的屬性,但我認爲您應該設置DP,即XAML綁定的屬性。喜歡這個? :

<Controls:MyTabControl 
    ... 
    Body="{Binding TheBody}" 
    Items="{Binding TheItems}"> 
    SelectedItem="{Binding TheSelectedItem}" /> 
+0

我不確定你是否正確理解我的意圖。我更新了這個問題,以便它(希望)更具描述性。 –

+0

對於'ItemsSource',你將Source設置爲指向MyTabControl,但對於ContentPresenter則不設置Source,所以我認爲它正在尋找DataContext中的SelectedItem ''MyTabControl',它通過繼承獲得,並且是無論如何指向。您可以嘗試 PScr

+0

好吧,但我仍然不知道如何綁定到外部的MyTabControl.DataContext,請看第二個代碼片段,尤其是「什麼是BINDING GOES HERE?」 '字符串:) –