ItemsControl只显示namespace.class,不呈现DataTemplate

时间:2022-02-22 14:17:47

I have the following problem: In a ItemsControl I have defined some DataTemplates for different classes. For no reason (or maybe I wasn't aware of it), just the class names will be rendered, instead of the Template. When I just use one Template with ItemTemplate and not DataTemplate it'll work. The "Element" classes are internal, a change to public won't do it:

我有以下问题:在ItemsControl中,我为不同的类定义了一些DataTemplates。无缘无故(或者我可能没有意识到),只会呈现类名而不是模板。当我只使用一个带有ItemTemplate的模板而不是DataTemplate时,它将起作用。 “元素”类是内部的,对公众的改变不会这样做:

    <ItemsControl ItemsSource="{Binding FilterElements}" >
                        <ItemsControl.Resources>
                            <DataTemplate DataType="local:DateFilter">
                                <StackPanel Orientation="Horizontal" MaxHeight="35">
                                    <Label Content="From " VerticalAlignment="Center" />
                                    <DatePicker SelectedDate="{Binding StartDate}" MaxWidth="120" BorderThickness="0"
                                                VerticalAlignment="Center" />
                                    <Label Content=" to " VerticalAlignment="Center" />
                                    <DatePicker SelectedDate="{Binding EndDate}" MaxWidth="120" BorderThickness="0"
                                                VerticalAlignment="Center" />
                                    <local:IconButton Icon="Resources/x-8x.png" Margin="0" />
                                </StackPanel>
                            </DataTemplate>

                            <DataTemplate DataType="local:TimeFilter">
                                <StackPanel Orientation="Horizontal" MaxHeight="35">
                                    <Label Content="Timespan: " VerticalAlignment="Center" />
                                    <xctk:TimePicker StartTime="{Binding StartTime}" EndTime="{Binding EndTime}"
                                                     MaxWidth="120" BorderThickness="0"
                                                     VerticalAlignment="Center" />
                                    <local:IconButton Icon="Resources/x-8x.png" Margin="0" />
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.Resources>
                    </ItemsControl>

1 个解决方案

#1


1  

When referencing a type, use x:Type

引用类型时,请使用x:Type

So, for your DateFilter DataTemplate:

所以,对于你的DateFilter DataTemplate:

<DataTemplate DataType="{x:Type local:DateFilter}">

​​tatemplate>

#1


1  

When referencing a type, use x:Type

引用类型时,请使用x:Type

So, for your DateFilter DataTemplate:

所以,对于你的DateFilter DataTemplate:

<DataTemplate DataType="{x:Type local:DateFilter}">

​​tatemplate>