Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在自定义控件中找不到提供DataContext的元素_Wpf_Custom Controls_Datacontext - Fatal编程技术网

Wpf 在自定义控件中找不到提供DataContext的元素

Wpf 在自定义控件中找不到提供DataContext的元素,wpf,custom-controls,datacontext,Wpf,Custom Controls,Datacontext,您好,我有一个自定义控件,用于显示其他xaml内容 在所述控件的实现中,我有一个用户控件,该控件期望视图模型绑定到其数据上下文,但是数据上下文始终为空,我收到以下错误消息: System.Windows.Data错误:3:找不到提供 数据上下文。BindingExpression:Path=OutComerStrictionVM; DataItem=null;目标元素是“XsdEnumRestrictionView”(名称=“”); 目标属性为“DataContext”(类型为“Object”)

您好,我有一个自定义控件,用于显示其他xaml内容

在所述控件的实现中,我有一个用户控件,该控件期望视图模型绑定到其数据上下文,但是数据上下文始终为空,我收到以下错误消息:

System.Windows.Data错误:3:找不到提供 数据上下文。BindingExpression:Path=OutComerStrictionVM; DataItem=null;目标元素是“XsdEnumRestrictionView”(名称=“”); 目标属性为“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>
我的实施是:

<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的丢失感到沮丧,并且想理解为什么我的自定义控件似乎没有一个属性

任何帮助都将不胜感激

谢谢


Kieran

好的,因此要显式绑定到DataContext,我必须将自定义控件的基类设置为HeaderedContentControl,并删除Header和Content属性。
现在一切似乎都很好,尽管我仍然不太明白当基类是控件时它为什么不起作用。

好的,所以要显式绑定到DataContext,我必须将自定义控件的基类设置为HeaderedContentControl,并删除Header和Content属性。 现在,它似乎工作得很好,尽管我仍然不太明白为什么基类是Control时它不工作

<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>