Wpf DataGridTemplateColumn找不到绑定根

Wpf DataGridTemplateColumn找不到绑定根,wpf,binding,datagrid,user-controls,Wpf,Binding,Datagrid,User Controls,我试图将名为simpleChart的用户控件放入DataGridTemplateColumn中。我将DataGrid绑定到一个ObservableCollection(其中索引是我为显示而创建的INotifyPropertyChanged类)。但是无论如何,绑定找不到根项。为了检查我的绑定,我在templateColumn旁边放了一个textColumn,它可以正常工作。以下是相关代码: <Window x:Class="TechniqueAnalysis.MainWindow"

我试图将名为simpleChart的用户控件放入DataGridTemplateColumn中。我将DataGrid绑定到一个
ObservableCollection
(其中索引是我为显示而创建的INotifyPropertyChanged类)。但是无论如何,绑定找不到根项。为了检查我的绑定,我在templateColumn旁边放了一个textColumn,它可以正常工作。以下是相关代码:

<Window x:Class="TechniqueAnalysis.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="" Height="549" Width="1744" 
    xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    xmlns:local="clr-namespace:Charts"
    xmlns:simpleChart ="clr-namespace:ChartOnGird;assembly=ChartOnGird"
    WindowState="Maximized" Closing="Window_Closing">
<Grid>
<DataGrid Margin="0,60,0,0" Name="dataGridWei" CanUserResizeColumns="False"  ItemsSource="{Binding}">
<DataGridTemplateColumn Header="2 day"  Width="100">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <simpleChart:SimpleChart X ="{Binding Path=X1, diagnostics:PresentationTraceSources.TraceLevel=High}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Value" Binding="{Binding Path=X1, diagnostics:PresentationTraceSources.TraceLevel=High, StringFormat=N2}"/>
</DataGrid>
</Grid>
</Window>

templatecolumn似乎无法找到正确的根项。问题是什么是帮助它找到合适的方法的合适方法?我试图添加RelateSource,但不知道如何添加。我的类“索引”在主窗口类下。请帮帮我。我花了两天的时间。我相信我就快到了,但是有一件事我无法在没有帮助的情况下解决。

我猜问题出在你的simpleChart
用户控件上。您可能以错误的方式为此控件设置了
DataContext
,这就是为什么绑定找不到
行.DataContext
,而是
UserControl.DataContext


请看一下,以正确的方式执行此操作。

您是否尝试过在DataTemplate上设置type属性?我不确定您的确切意思。但在将类“Index”移到主窗口之外之后,我尝试添加DataTemplate DataType=“simpleChart:simpleChart”或DataType=“local:Index”。他们两个都没有帮助。我仍然得到完全相同的结果,我想你可能是对的。但我不知道怎么做,即使在阅读了你链接的页面后。我把DataContext单独留下了。但是在我在usercontrol中指定DataContext之后:this.DataContext=this。它仍然找到了数据上下文元素:SimpleChart,但现在它使用根项目SimpleChart激活,正如我所指定的。当我查看您给出的示例时,它还发现数据上下文元素为UserControl1,但由于某些原因,根项是正确的对象ModelObject。您是对的,很久以前我在XAML中的控件定义中错误地放置了DataContext=“”。这就是为什么它给我带来这么多麻烦。谢谢
System.Windows.Data Warning: 58 : BindingExpression (hash=45649854): Default mode resolved to OneWay
System.Windows.Data Warning: 59 : BindingExpression (hash=45649854): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 60 : BindingExpression (hash=45649854): Attach to ChartOnGird.SimpleChart.X (hash=40977533)
System.Windows.Data Warning: 65 : BindingExpression (hash=45649854): Resolving source
System.Windows.Data Warning: 68 : BindingExpression (hash=45649854): Found data context element: SimpleChart (hash=40977533) (OK)
System.Windows.Data Warning: 76 : BindingExpression (hash=45649854): Activate with root item ''
System.Windows.Data Warning: 106 : BindingExpression (hash=45649854):   At level 0 - for String.X1 found accessor <null>
System.Windows.Data Warning: 106 : BindingExpression (hash=45649854):   At level 0 - for EnumerableCollectionView.X1 found accessor <null>
System.Windows.Data Warning: 102 : BindingExpression (hash=45649854): Replace item at level 0 with {NullDataItem}, using accessor {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 78 : BindingExpression (hash=45649854): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 86 : BindingExpression (hash=45649854): TransferValue - using fallback/default value '9'
System.Windows.Data Warning: 87 : BindingExpression (hash=45649854): TransferValue - using final value '9'
System.Windows.Data Warning: 54 : Created BindingExpression (hash=32948864) for Binding (hash=33575416)
System.Windows.Data Warning: 58 : BindingExpression (hash=32948864): Default mode resolved to OneWay
System.Windows.Data Warning: 59 : BindingExpression (hash=32948864): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 60 : BindingExpression (hash=32948864): Attach to System.Windows.Controls.TextBlock.Text (hash=6303902)
System.Windows.Data Warning: 65 : BindingExpression (hash=32948864): Resolving source
System.Windows.Data Warning: 68 : BindingExpression (hash=32948864): Found data context element: TextBlock (hash=6303902) (OK)
System.Windows.Data Warning: 69 : BindingExpression (hash=32948864): DataContext is null
System.Windows.Data Warning: 63 : BindingExpression (hash=32948864): Resolve source deferred
System.Windows.Data Warning: 65 : BindingExpression (hash=8194399): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=8194399): Found data context element: TextBlock (hash=33203677) (OK)
System.Windows.Data Warning: 76 : BindingExpression (hash=8194399): Activate with root item Index (hash=33903126)
System.Windows.Data Warning: 106 : BindingExpression (hash=8194399):   At level 0 - for Index.X1 found accessor RuntimePropertyInfo(X1)
System.Windows.Data Warning: 102 : BindingExpression (hash=8194399): Replace item at level 0 with Index (hash=33903126), using accessor RuntimePropertyInfo(X1)
System.Windows.Data Warning: 99 : BindingExpression (hash=8194399): GetValue at level 0 from Index (hash=33903126) using RuntimePropertyInfo(X1): '1'
System.Windows.Data Warning: 78 : BindingExpression (hash=8194399): TransferValue - got raw value '1'
System.Windows.Data Warning: 82 : BindingExpression (hash=8194399): TransferValue - implicit converter produced '1.00'
System.Windows.Data Warning: 87 : BindingExpression (hash=8194399): TransferValue - using final value '1.00'
System.Windows.Data Warning: 65 : BindingExpression (hash=32948864): Resolving source
System.Windows.Data Warning: 68 : BindingExpression (hash=32948864): Found data context element: TextBlock (hash=6303902) (OK)
System.Windows.Data Warning: 76 : BindingExpression (hash=32948864): Activate with root item Index (hash=49245678)
System.Windows.Data Warning: 105 : BindingExpression (hash=32948864):   At level 0 using cached accessor for Index.X1: RuntimePropertyInfo(X1)
System.Windows.Data Warning: 102 : BindingExpression (hash=32948864): Replace item at level 0 with Index (hash=49245678), using accessor RuntimePropertyInfo(X1)
System.Windows.Data Warning: 99 : BindingExpression (hash=32948864): GetValue at level 0 from Index (hash=49245678) using RuntimePropertyInfo(X1): '1'
System.Windows.Data Warning: 78 : BindingExpression (hash=32948864): TransferValue - got raw value '1'
System.Windows.Data Warning: 82 : BindingExpression (hash=32948864): TransferValue - implicit converter produced '1.00'
System.Windows.Data Warning: 87 : BindingExpression (hash=32948864): TransferValue - using final value '1.00'