有沒有辦法在破壞時重新啓用xaml中的屬性繼承?如何恢復xaml中破損的屬性繼承
這裏是CustomControl:
public class CustomControl : DataGrid
{
static CustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
}
public object Item
{
get { return (object)GetValue(ItemProperty); }
set { SetValue(ItemProperty, value); }
}
public static readonly DependencyProperty ItemProperty =
DependencyProperty.Register("Item", typeof(object), typeof(CustomControl), new PropertyMetadata(null));
public object ContentControl
{
get { return (object)GetValue(ContentControlProperty); }
set { SetValue(ContentControlProperty, value); }
}
public static readonly DependencyProperty ContentControlProperty =
DependencyProperty.Register("ContentControl", typeof(object), typeof(CustomControl), new PropertyMetadata(OnContentChanged));
private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as CustomControl;
if (e.OldValue != null)
{
control.RemoveLogicalChild(e.OldValue);
}
control.AddLogicalChild(e.NewValue);
}
}
XAML中的一部分。 (Generic.xaml)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication">
<Style TargetType="{x:Type local:CustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl}">
<ContentPresenter DataContext="{TemplateBinding Item}" ContentSource="ContentControl"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
而這是主窗口:
<TabControl>
<TabItem Header="Skip this tab...">
</TabItem>
<TabItem Header="Here...">
<WpfApplication:CustomControl DataContext="{Binding}" x:Name="CustomControl" Item="{Binding InnerViewModel}">
<WpfApplication:CustomControl.ContentControl>
<!-- The DataContext is not inherited... -->
<TextBlock Text="{Binding InnerName}" />
</WpfApplication:CustomControl.ContentControl>
</WpfApplication:CustomControl>
</TabItem>
</TabControl>
最後,對應的ViewModels:
public class ViewModel
{
public string Name { get; set; }
public InnerViewModel InnerViewModel { get; set; }
public ViewModel()
{
InnerViewModel = new InnerViewModel();
Name = "ViewModel";
}
}
public class InnerViewModel
{
public string InnerName { get; set; }
public InnerViewModel()
{
InnerName = "InnerViewModel";
}
}
在此先感謝!
謝謝你,但我只用1個含量DP爲簡單起見,使這個例子,所以使長話短說,我有一個包含多個內容dependecy屬性 - 一個用於添加DataGrid的內容,一個是編輯和一個用於刪除。在這種情況下,無法放置多個[ContentProperty]屬性。 – user3049133
噢好吧,在這種情況下,您可以離開ContentPropertyAttribute,並刪除綁定到DataContext,你應該沒問題。 –
DataContext不是InnerViewModel的「Item」。如果仔細看一下Generic.xaml,你會看到「 」。 DataContext =「{TemplateBinding Item}」的部分不起作用,如果我註釋掉control.AddLogicalChild(e.NewValue);它將工作。但是這會破壞我使用元素名稱綁定的能力。 –
user3049133