2017-05-30 52 views
0

我有一個XamGrid,有兩列,NameType。根據Type,我想爲Name設置不同種類的列,因此我使用的是TemplateColumn。在數據模板中,我有一個ContentControl,默認ContentTemplateDataTrigger,如果Type是特定值,則ContentTemplate設置爲不同的列樣式。 我將TemplateColumn的全部四個模板(ItemTemplate,EditorTemplate,AddNewRowItemTemplate,AddNewRowEditorTemplate)設置爲此數據模板。XamGrid中TemplateColumn的EditorTemplate不起作用

ItemTemplateAddNewRowItemTemplateAddNewRowEditorTemplate工作如預期,但EditorTemplate不,見附件圖片:

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

<code>AddNewRowEditorTemplate</code>

<code>EditorTemplate</code>

這裏是我的代碼:

個MainWindow.xaml:

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

MainWindow.xaml.cs:

using System.Collections.ObjectModel; 

namespace WpfApplication1 
{ 
    public partial class MainWindow 
    { 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    public ObservableCollection<Data> DataCollection { get; } = new ObservableCollection<Data> 
    { 
     new Data { Name = "Foo", Type = "bool" }, 
     new Data { Name = "Bar", Type = "enum" } 
    }; 
    } 

    public class Data 
    { 
    public string Name { get; set; } 
    public string Type { get; set; } 
    } 
} 

回答

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}" />