XamGrid中的TemplateColumn的EditorTemplate无法正常工作

时间:2017-05-30 07:37:49

标签: wpf datatemplate infragistics xamgrid

我有一个包含两列的XamGrid,NameType。根据{{​​1}},我希望为Type提供不同类型的列,因此我使用的是Name。在数据模板中,如果TemplateColumn是特定值,我有一个ContentControl,其中包含默认ContentTemplateDataTrigger,可将ContentTemplate设置为不同的列样式。 我正在为此数据模板设置Type的四个模板(ItemTemplateEditorTemplateAddNewRowItemTemplateAddNewRowEditorTemplate)。

TemplateColumnItemTemplateAddNewRowItemTemplate按预期工作,但AddNewRowEditorTemplate没有,请参阅附图:

<code>ItemTemplate</code> and <code>AddNewRowItemTemplate</code>

<code>AddNewRowEditorTemplate</code>

<code>EditorTemplate</code>

这是我的代码:

MainWindow.xaml:

EditorTemplate

MainWindow.xaml.cs:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ig="http://schemas.infragistics.com/xaml"
        Width="640" Height="480" >
    <Window.Resources>
        <DataTemplate x:Key="EditorTemplate">
            <TextBox Width="64"/>
        </DataTemplate>
        <DataTemplate x:Key="BoolEditorTemplate">
            <CheckBox/>
        </DataTemplate>
        <DataTemplate x:Key="DataTemplate">
            <ContentControl Content="{Binding }">
                <ContentControl.Style>
                    <Style TargetType="{x:Type ContentControl}">
                        <Setter Property="ContentTemplate" Value="{StaticResource EditorTemplate}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Type}" Value="bool">
                                <Setter Property="ContentTemplate" Value="{StaticResource BoolEditorTemplate}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </DataTemplate>
    </Window.Resources>
    <ig:XamGrid ItemsSource="{Binding DataCollection, RelativeSource={RelativeSource AncestorType=Window}}"
                AutoGenerateColumns="False">
        <ig:XamGrid.EditingSettings>
            <ig:EditingSettings AllowEditing="Row" />
        </ig:XamGrid.EditingSettings>
        <ig:XamGrid.AddNewRowSettings>
            <ig:AddNewRowSettings AllowAddNewRow="Top" />
        </ig:XamGrid.AddNewRowSettings>

        <ig:XamGrid.Columns>
            <ig:TemplateColumn Key="Name"
                               ItemTemplate="{StaticResource DataTemplate}"
                               AddNewRowItemTemplate="{StaticResource DataTemplate}"
                               EditorTemplate="{StaticResource DataTemplate}"
                               AddNewRowEditorTemplate="{StaticResource DataTemplate}"/>
            <ig:TextColumn Key="Type"/>
        </ig:XamGrid.Columns>
    </ig:XamGrid>
</Window>

1 个答案:

答案 0 :(得分:0)

正如here on the infragistics forum所述,对于此用例,不仅需要EditorTemplate,还需要EditorStyle

<Style x:Key="EditorStyle" TargetType="{x:Type ContentControl}">
    <Setter Property="ContentTemplate" Value="{StaticResource EditorTemplate}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Type}" Value="bool">
            <Setter Property="ContentTemplate" Value="{StaticResource BoolEditorTemplate}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<DataTemplate x:Key="DataTemplate">
    <ContentControl Content="{Binding }"
                    Style="{StaticResource EditorStyle}" />>
</DataTemplate>

[...]

<ig:TemplateColumn Key="Name"
                   ItemTemplate="{StaticResource DataTemplate}"
                   AddNewRowItemTemplate="{StaticResource DataTemplate}"
                   EditorTemplate="{StaticResource DataTemplate}"
                   AddNewRowEditorTemplate="{StaticResource DataTemplate}"
                   EditorStyle="{StaticResource EditorStyle}" />