您好我有一个自定义控件,用于显示其他xaml内容。
在所述控件的实现中,我有一个用户控件,希望将视图模型绑定到其数据上下文,但是数据上下文始终为null,并且我收到以下错误消息:
System.Windows.Data错误:3:找不到提供的元素 DataContext的。 BindingExpression:路径= OutcomeRestrictionVM; 的DataItem = NULL; target元素是'XsdEnumRestrictionView'(Name =''); target属性是'DataContext'(类型'Object')
我的自定义控件的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的丢失感到沮丧,并且想要理解为什么我的自定义控件似乎没有。
非常感谢任何帮助
由于
基兰
答案 0 :(得分:0)
好的,为了显式绑定DataContext,我必须将自定义控件的基类设置为HeaderedContentControl并删除Header和Content属性。 现在一切似乎工作正常,但我还是不太明白为什么当基类是Control时它不起作用。