1

我正在玩 Infragistics xamDataGrid。我想在“字段”(=单元格)中显示一个自定义用户控件,并为其提供字段的 DataContext。不知何故 DataContext 总是为空:-)

这是 XAML:

<UserControl.Resources>
  <Style x:Key="MyTestUserControl" TargetType="{x:Type igDP:CellValuePresenter}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
          <myUC:MyTestUserControl
              DataContext="{Binding RelativeSource={RelativeSource TemplatedParent},
                                    Path=Value}" />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  <Style x:Key="TextBoxField" TargetType="{x:Type igDP:CellValuePresenter}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
          <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent},
                                  Path=Value}" />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</UserControl.Resources>

TextBoxField 的 DataContext 工作正常,但 MyUserControl 的 DataContext 不能。

我在其中定义字段的更多 XAML:

<igDP:UnboundField BindingPath="SimpleTestStringToDisplay" Label="UnboundField">
  <igDP:Field.Settings>
    <igDP:FieldSettings CellValuePresenterStyle="{StaticResource TextBoxField}" />
  </igDP:Field.Settings>
</igDP:UnboundField>

<igDP:UnboundField BindingPath="MyUserControlViewModel"
                   Label="UnboundField (UserControl)">
  <igDP:Field.Settings>
    <igDP:FieldSettings CellValuePresenterStyle="{StaticResource MyTestUserControl}" />
  </igDP:Field.Settings>
</igDP:UnboundField>

有谁知道我做错了什么?

谢谢!

干杯,约瑟夫

编辑:我也尝试过这样的事情,但没有成功:

  <myUC:MyUserControl DataContext="{TemplateBinding DataContext}" />
4

1 回答 1

0

您上面的测试项目没有相同的绑定,它在模板中但不在 <igDP:UnboundField>

尝试使用相同的绑定,看看您是否仍然没有获得数据上下文,那么至少该实验将是有效的(孤立的)。也许问题出在您的视图模型绑定上,请尝试以下代码 -

<igDP:UnboundField BindingPath="MyUserControlViewModel" Label="UnboundField" >
  <igDP:Field.Settings>
    <igDP:FieldSettings CellValuePresenterStyle="{StaticResource TextBoxField}" />
  </igDP:Field.Settings>
</igDP:UnboundField>

<igDP:UnboundField BindingPath="MyUserControlViewModel" Label="UnboundField (UserControl)">
  <igDP:Field.Settings>
    <igDP:FieldSettings CellValuePresenterStyle="{StaticResource MyTestUserControl}" />
  </igDP:Field.Settings>
</igDP:UnboundField>

如果你这样做,两个模板都没有得到它们的数据上下文吗?在模板中的绑定上放置一个debugConverter(方法 2),以确保您的绑定正在发生。

顺便说一句,您在输出窗口中收到了哪些绑定失败消息?

于 2009-11-15T12:03:02.033 回答