2012-07-03 60 views
0

我想把一個名爲simpleChart的用戶控件放到DataGridTemplateColumn中。我將DataGrid綁定到ObservableCollection<Index>(其中Index是我創建的用於顯示的INotifyPropertyChanged類)。但不知何故,綁定無法找到根項目。爲了檢查我的綁定,我把一個textColumn放在templateColumn的旁邊,它工作得很好。下面是相關代碼:DataGridTemplateColumn找不到綁定根

<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類下。請幫幫我。我花了2天時間。我相信我幾乎在那裏,但只有一些我沒有幫助就找不到的東西。

+0

您是否嘗試過在DataTemplate上設置type-property? – Silvermind

+0

我不確定你的意思。但我在我的類「Index」移動到MainWindow之外後,試圖在DataTemplate DataType =「simpleChart:simpleChart」或DataType =「local:Index」中添加。他們兩個都沒有幫助。我仍然得到完全相同的輸出 – Bob

回答

0

我猜問題是你的simpleChart UserControl。可能您以錯誤的方式爲此控件設置DataContext,這就是爲什麼綁定找不到Row.DataContext而是UserControl.DataContext

看看這Simple Pattern for Creating Re-useable UserControls做正確的方法。

+0

我認爲你可能是對的。但即使在閱讀您鏈接的頁面之後,我也不確定如何操作。我原來只留下了DataContext。但是在我的用戶控件中指定了DataContext之後:this.DataContext = this。它仍然找到了數據上下文元素:SimpleChart,但現在它按照我指定的方式與根項SimpleChart一起激活。當我看着你給出的例子時,它也發現數據上下文元素爲UserControl1,但由於某種原因,根項目是正確的對象ModelObject。 – Bob

+0

你是對的,我很久以前在XAML的控制定義中錯誤地放了一個DataContext =「」。這就是爲什麼它給我帶來很多麻煩。謝謝。 – Bob

相關問題