无法使ItemTemplateSelector工作

时间:2012-12-01 00:17:46

标签: c#

我一直在努力让ItemTemplateSelector工作,但没有成功。这是我的代码。任何帮助将非常感激。感谢

的Xaml: > DataTemplate x:Key =“EmpItemTemplate”>

      <StackPanel Orientation="Vertical">
            <TextBlock Text="hello"></TextBlock>
            <TextBlock Text="Availability"></TextBlock>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="ScheduleItemTemplate">
        <StackPanel Orientation="Vertical">
            <TextBlock Text="Shift"></TextBlock>
            <TextBlock Text="hello"></TextBlock>
        </StackPanel>
    </DataTemplate>        
     

代码背后: 命名空间ScheduleHelper {     class ScheduleTemplateSelector:DataTemplateSelector     {         public DataTemplate EmployeeTemplate {get;组; }         public DataTemplate ScheduleTemplate {get;组; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = (FrameworkElement)container;

        if (element != null && item != null)
        {
            return EmployeeTemplate;
        }

        return ScheduleTemplate;
    }
}

}

//创建数据表             DataTable dt = ds.Tables [“EmpAvailability”];

        //Create GroupStyle
        GroupStyle gs = new GroupStyle();
        gs.ContainerStyle = (Style)Resources["GroupStyle"];

        //Create Datagrid
        DataGrid dtg = new DataGrid();
        dtg.GroupStyle.Add(gs);
        dtg.HeadersVisibility = DataGridHeadersVisibility.None;
        dtg.ItemsSource = dt.DefaultView;
        dtg.Items.GroupDescriptions.Add(new PropertyGroupDescription("PositionName"));
        dtg.ItemTemplateSelector = (DataTemplateSelector)Resources["myTemplateSelector"];
        dtg.AutoGeneratedColumns +=new EventHandler(dtg_AutoGeneratedColumns);
        WeeklyViewskp.Children.Add(dtg);

0 个答案:

没有答案