开发者

DataBinding of a UserControl via ControlTemplate

I'm playing around with the Infragistics xamDataGrid. I'd like to display in a "Field" (= Cell) a custom UserControl and have for it the Field's DataContext. Somehow the DataContext is always null :-)

Here is the 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>

The DataContext for the TextBoxField works fine, but the DataContext for the MyUserControl doesn't.

Some more XAML where I define the Fields:

<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>

Does anyone have an idea what I'm doing wrong?

Thanks!

Cheers, Joseph

EDIT: I also tried something like this, without success:

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


your test project above does not have the same bindings, it does in the templates but not on the <igDP:UnboundField>

try it using the same bindings and see if you still dont get a data context, then at least the experiment will be valid (isolated). maybe the issue is with your view model binding, try the follwing code -

<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>

if you do this do both the templates not get their data context?. put a debugConverter (method 2) on your binding in the template, to make sure youre binding is happening.

By the way, what binding failure messages are you getting in your output window?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜