2012-08-07 41 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

相关问题