您好我有一个自定义控件,用于呈现其他 xaml 内容。
在所述控件的实现中,我有一个用户控件,它期望视图模型绑定到其数据上下文,但是数据上下文始终为空,并且我收到以下错误消息:
System.Windows.Data 错误:3:找不到提供 DataContext 的元素。BindingExpression:Path=OutcomeRestrictionVM; 数据项=空;目标元素是'XsdEnumRestrictionView'(名称='');目标属性是“DataContext”(类型“对象”)
我的自定义控件的 Generic.xaml 是:
<ControlTemplate TargetType="{x:Type local:ContextGroupBox}" x:Key="ContextGroupBoxTemplate">
<Grid SnapsToDevicePixels="true" >
<Grid.Resources>
<ControlTemplate x:Key="tplFlatButton" TargetType="{x:Type Button}">
<Border Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
TextElement.Foreground="{TemplateBinding Foreground}"
TextElement.FontFamily="{TemplateBinding FontFamily}"
TextElement.FontSize="{TemplateBinding FontSize}"
TextElement.FontStretch="{TemplateBinding FontStretch}"
TextElement.FontWeight="{TemplateBinding FontWeight}"/>
</Border>
</ControlTemplate>
<Style x:Key="stlFlatButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Template" Value="{StaticResource tplFlatButton}" />
</Style>
</Grid.Resources>
<Rectangle RadiusX="10" RadiusY="10" StrokeThickness="{TemplateBinding BorderThickness}" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}"/>
<Grid Margin="{TemplateBinding BorderThickness}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Group Title -->
<Grid Grid.Row="0" Margin="5,5,5,1" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter Content="{TemplateBinding Header}" RecognizesAccessKey="true" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" TextElement.Foreground="{TemplateBinding Foreground}" TextElement.FontSize="{TemplateBinding HeaderFontSize}" TextElement.FontWeight="Bold"/>
<!--<TextBlock Grid.Column="0" Text="Record Access" Foreground="#4E86B8" FontWeight="Bold" FontSize="14" VerticalAlignment="Center"/>-->
<Button Command="{TemplateBinding ShowHelpCommand}" CommandParameter="{TemplateBinding ShowHelpParameter}" Style="{StaticResource stlFlatButton}" Grid.Column="1" HorizontalAlignment="Right">
<Image Source="/Hornbill.Resources.Image;component/Shared/information.png" Width="16" Height="16" Visibility="Visible" >
</Image>
</Button>
</Grid>
<ContentControl DataContext="{TemplateBinding DataContext}" Content="{TemplateBinding Content}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Grid.Row="1" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type local:ContextGroupBox}">
<Setter Property="Template" Value="{StaticResource ContextGroupBoxTemplate}" />
<Setter Property="Padding" Value="7,4,7,7" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
自定义控件 ContextGroupBox.cs
public static double defaultHeaderFontSize = 14;
public double HeaderFontSize
{
get { return (double)GetValue(HeaderFontSizeProperty); }
set { SetValue(HeaderFontSizeProperty, value); }
}
// Using a DependencyProperty as the backing store for HeaderFontSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderFontSizeProperty =
DependencyProperty.Register("HeaderFontSize", typeof(double), typeof(ContextGroupBox), new PropertyMetadata(defaultHeaderFontSize));
public object Header
{
get { return (object)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
// Using a DependencyProperty as the backing store for Header. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(object), typeof(ContextGroupBox));
public FrameworkElement Content
{
get { return (FrameworkElement)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
// Using a DependencyProperty as the backing store for Content. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(FrameworkElement), typeof(ContextGroupBox));
public ICommand ShowHelpCommand
{
get { return (ICommand)GetValue(ShowHelpCommandProperty); }
set { SetValue(ShowHelpCommandProperty, value); }
}
// Using a DependencyProperty as the backing store for ShowHelpCommand. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShowHelpCommandProperty =
DependencyProperty.Register("ShowHelpCommand", typeof(ICommand), typeof(ContextGroupBox));
public object ShowHelpParameter
{
get { return (object)GetValue(ShowHelpParameterProperty); }
set { SetValue(ShowHelpParameterProperty, value); }
}
// Using a DependencyProperty as the backing store for ShowHelpParameter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShowHelpParameterProperty =
DependencyProperty.Register("ShowHelpParameter", typeof(object), typeof(ContextGroupBox));
static ContextGroupBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ContextGroupBox), new FrameworkPropertyMetadata(typeof(ContextGroupBox)));
}
我的实现是:
<hctrl:ContextGroupBox Grid.Column="1" DataContextChanged="gbOutcomeParam_DataContextChanged_1" Header="{l:Translate Outcome}" Name="gbOutcomeParam" VerticalAlignment="Stretch" Margin="5 5">
<ctrl:XsdEnumRestrictionView DataContext="{Binding OutcomeRestrictionVM, Mode=OneWay}" DataContextChanged="XsdEnumRestrictionView_DataContextChanged" />
</hctrl:ContextGroupBox>
疯狂的是,如果我在我的用户控件 (ctrl:XsdEnumRestrictionView) 中创建一个依赖属性并绑定到该属性,一切正常,但当我显式绑定到 DataContext 时却不行。
好的,也许用户控件无论如何都应该使用属性,但我对丢失 DataContext 感到沮丧,并想了解为什么我的自定义控件似乎没有属性。
任何帮助都感激不尽
谢谢
基兰