【问题标题】:DataBinding of a UserControl via ControlTemplate通过 ControlTemplate 对 UserControl 进行数据绑定
【发布时间】:2009-11-12 17:28:32
【问题描述】:

我正在使用 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}" />

【问题讨论】:

    标签: c# wpf data-binding user-controls controltemplate


    【解决方案1】:

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

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

    <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 (method 2),以确保您的绑定正在发生。

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

    【讨论】:

    • 感谢您的回答。实际上,我不必在 UserControl 中添加任何内容。如果在此处正确声明,它只会从 UnboundField 中“继承” DataContext ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 2019-05-08
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    相关资源
    最近更新 更多