2012-08-07 44 views
3

productColumn2的綁定完美地適用於這兩種方式。當我爲每個添加一個轉換器時,productColumn1稱爲轉換器;但是當從可觀察集合加載時將其值設置爲null,或者在賦值時將值設置爲產品(但實際上並未分配可觀察集合)。相同的綁定適用於1個XAML項目,但爲另一個爲空

問題與DataContext和LogicalTree有關。 ProductSelectorTextBoxUserControl的DataContext本身就是它自己的代碼。我希望能夠將其「文本」屬性綁定到我的可觀察集合,如productColumn2中。我到目前爲止似乎無法將ProductSelectorTextBoxUserControl DataContext設置爲此處使用的DataContext。

<DataGrid ItemsSource="{Binding Path=ObservableCollectionItems, Mode=OneWay}" AutoGenerateColumns="False" EnableRowVirtualization="True" > 
<DataGrid.Columns> 
    <DataGridTemplateColumn x:Name="productColumn1" SortMemberPath="Product" > 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <productSelector:ProductSelectorTextBoxUserControl Text="{Binding Path=Product, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=LostFocus, ValidatesOnExceptions=True}" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTextColumn x:Name="productColumn2" Binding="{Binding Path=Product, Mode=TwoWay, NotifyOnSourceUpdated=True}" />    
</DataGrid.Columns> 

+0

您能分享轉換器的代碼嗎? – sellmeadog 2012-08-07 15:45:44

+0

在輸出窗口中獲取任何綁定錯誤? – 2012-08-07 16:05:08

+0

您是否曾嘗試在'productColumn2'中使用轉換器?我最初的猜測是'customeTextBoxOfProductType'沒有找到'Product'屬性,這會指示導航邏輯樹時出現問題而無法找到'DataGrid.DataContext'。 – sellmeadog 2012-08-07 16:09:05

回答

0

謝謝@SellMeADog幫助我解決這個問題,但是這仍然讓我想辦法弄清楚。最後一行是:

<productSelector:ProductSelectorTextBoxUserControl x:Name="productSelector" Product="{Binding Path=Item.Product, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow, AncestorLevel=1}, Converter={StaticResource productNameToProductConverter}, Mode=TwoWay, NotifyOnSourceUpdated=True, ValidatesOnExceptions=True}" /> 

關鍵點是RelativeSource DataGridRow,Path是Item(ObservableCollection)。物業

如果您發現問題涉及到文本,這是指產品,我不得不切換到產品和添加轉換器。 UserControl將設置自己的文本

+0

還UpdateSourceTrigger = LostFocus是個壞主意,並且做了這個1,必須刪除或默認爲我的情況制定2路 – Brent 2012-08-09 15:47:56

2

如果ProductSelectorTextBoxUserControlDataContext設置爲本身的Binding將無法​​找到,因爲它不存在Product財產。您需要修改綁定,以便它知道在哪裏可以找到Product屬性;這樣的事情可能工作:

<productSelector:ProductSelectorTextBoxUserControl Text="{Binding Path=Product, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=LostFocus, ValidatesOnExceptions=True}" /> 

通過添加RelativeSource,你告訴的結合,尋找在DataGridRow.DataContextProduct財產。

UPDATE

你試過{RelativeSource AncestorType={x:Type DataGridRow}}?您應該定位到該行而不是網格。

ItemContainerGenerator創建每個DataGridRow時,它會將該行的DataContext設置爲ObservableCollectionItems中的相應項目。因此,邏輯樹是這樣的:

  • DataGrid(的DataContext =對象,定義ObservableCollectionItems
    • DataGridRow(的DataContext = ObservableCollectionItems[0]
      • ProductSelectorTextBoxUserControl(的DataContext =個體經營)
    • DataGridRow(DataContext = ObservableCollectionItems[0]
      • ProductSelectorTextBoxUserControl(的DataContext =個體經營)

網格的DataContext不公開Product屬性,它集合中的每個元素定義(除非我已經錯過了一些東西)。 Product屬性應該位於每行的上下文中。

+0

感謝您澄清它是如何工作的,是的,我曾嘗試DataGridRow並無法讓它工作,我會發布具體信息 – Brent 2012-08-08 17:09:19

+0

System.Windows.Data錯誤:40:BindingExpression路徑錯誤:在'object'''DataGridRow'(Name ='')'找不到'Product'屬性。 BindingExpression:路徑=產品; DataItem ='DataGridRow'(Name ='');目標元素是'ProductSelectorTextBoxUserControl'(Name ='productSelector');目標屬性是'文本'(類型'String') | Brent 2012-08-08 17:09:51

+0

對不起,我無法提供更多幫助,但我不知道還有什麼建議。我可以提供的最佳建議是研究在「CellTemplate」中使用「UserControl」時對數據綁定的影響。問題也可能在於實施您的控制。將'DataContext'設置爲控件本身並不常見。不知道你爲什麼這樣做,但我會建議重新設計你的控制,所以沒有必要。 – sellmeadog 2012-08-08 17:26:16

相關問題