0

我正在尝试将名为 simpleChart 的用户控件放入 DataGridTemplateColumn。我将 DataGrid 绑定到一个ObservableCollection<Index>(其中 Index 是我为显示创建的 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>

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

模板列似乎无法找到正确的根项。问题是什么是帮助它找到正确的正确方法?我试图添加一个 RelateSource 但没有弄清楚如何。我的类“索引”在 MainWindow 类下。请帮我。我花了2天时间。我相信我快到了,但有些事情在没有帮助的情况下我无法弄清楚。

4

1 回答 1

0

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

看看这个用于创建可重用用户控件的简单模式,以正确的方式进行操作。

于 2012-07-03T22:20:13.363 回答