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