DataGridTemplateColumn找不到绑定根

时间:2012-07-03 20:38:13

标签: wpf binding datagrid user-controls

我正在尝试将名为simpleChart的用户控件放入DataGridTemplateColumn中。我将DataGrid绑定到ObservableCollection<Index>(其中Index是我为显示创建的INotifyPropertyChanged类)。但不知何故,绑定无法找到根项。为了检查我的绑定,我把一个textColumn放在templateColumn旁边,它运行得很好。以下是相关代码:

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

数据绑定是通过后台代码将ObservableCollection绑定到datagrid的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'

似乎templatecolumn无法找到正确的根项。问题是什么是帮助它找到合适的方法的正确方法?我试图添加一个RelateSource,但没有弄清楚如何。我的班级“索引”在MainWindow类下。请帮我。我花了两天时间。我相信我几乎就在那里,但有一些我无法帮助的事情。

1 个答案:

答案 0 :(得分:0)

我猜这个问题是你的simpleChart UserControl。可能是您以错误的方式为此控件设置了DataContext,这就是绑定找不到Row.DataContext而是UserControl.DataContext的原因。

看看这个Simple Pattern for Creating Re-useable UserControls以正确的方式做到这一点。