自定義控件已經自定義控件的內容定義如下:創建在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>
我怎樣才能建立ContentControl
的Content
綁定,以便它指向中定義的內容210?
也許有些人會問爲什麼我創建擺在首位的自定義選項卡控制 - 答案是:因爲我必須做一些不規範 - 分組標籤,着色等周圍拖動......
編輯:
我在這裏的目標是MyTabControl
表現得像一個標準的WPF TabControl
- 所以它的Items
或ItemsSource
應綁定到某個標籤的一些收集和內容應該綁定到任何當前選擇。爲了實現這一點,我在我的控制代碼後面綁定了ListView
到Items
集合,並試圖將ContentPresenter
的DataContext
綁定到當前選定的任何內容(它也在代碼後面設置)。問題是,如果我在應用程序的某個地方使用MyTabControl.Body
,我不知道如何綁定ContentControl.Content
屬性,以便它可以從我的自定義控件中獲取它的數據。
我不確定你是否正確理解我的意圖。我更新了這個問題,以便它(希望)更具描述性。 –
對於'ItemsSource',你將Source設置爲指向MyTabControl,但對於ContentPresenter則不設置Source,所以我認爲它正在尋找DataContext中的SelectedItem ''MyTabControl',它通過繼承獲得,並且是無論如何指向。您可以嘗試
PScr
好吧,但我仍然不知道如何綁定到外部的MyTabControl.DataContext,請看第二個代碼片段,尤其是「什麼是BINDING GOES HERE?」 '字符串:) –