Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何在特定位置禁用DataTemplate?_Wpf_Datatemplate - Fatal编程技术网

Wpf 如何在特定位置禁用DataTemplate?

Wpf 如何在特定位置禁用DataTemplate?,wpf,datatemplate,Wpf,Datatemplate,考虑以下模型: var model = new object[] { "Ala ma kota", DateTime.Now }; 现在,让我们假设,我们有以下用于显示此模型的ListBox的数据模板: <ListBox ItemsSource="{Binding Data}"> <ListBox.Resources> <DataTemplate DataType="{x:Type sys:String}">

考虑以下模型:

var model = new object[] { "Ala ma kota", DateTime.Now };
现在,让我们假设,我们有以下用于显示此模型的ListBox的数据模板:

<ListBox ItemsSource="{Binding Data}">
    <ListBox.Resources>
        <DataTemplate DataType="{x:Type sys:String}">
            <StackPanel Orientation="Horizontal">
                <TextBlock>String:</TextBlock>
                <TextBlock Text="{Binding}" />
            </StackPanel>
        </DataTemplate>

        <DataTemplate DataType={x:Type sys:DateTime}">
            <StackPanel Orientation="Horizontal">
                <TextBlock>Data i czas:</TextBlock>
                <Calendar DisplayDate={Binding Mode=OneWay}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

字符串:
如果我们运行这样的程序,会得到非常有趣的效果:


有没有办法在某种程度上禁用
DataTemplate
?我想在第二个数据模板中“阻止”它,以便正确显示日历。

我可以想到的一种方法是利用WPF中的资源查找行为。它向上遍历可视化树,直到找到控件的默认资源

您可以在本地为DateTime的dataTemplate内的字符串定义资源,以便它在本地应用于您的DateTime模板,而对于其他模板,它的行为将继续相同:

            <DataTemplate DataType="{x:Type sys:DateTime}">
                <StackPanel Orientation="Horizontal">
                    <StackPanel.Resources>
                        <DataTemplate DataType="{x:Type sys:String}">
                            <TextBlock Text="{Binding}"/>
                        </DataTemplate>
                    </StackPanel.Resources>
                    <TextBlock>Data i czas:</TextBlock>
                    <Calendar DisplayDate="{Binding Mode=OneWay}" />
                </StackPanel>
            </DataTemplate>

数据一czas:
输出:


@RohitVats我想告诉WPF,当模板化类型为
DateTime
的元素时,不要为
String
搜索
数据模板。简单地说:我希望
日历
能够正确显示:)好的,现在就可以了。如果有帮助,请查看我的答案+1-解决效果问题。但是有没有办法解决这个问题呢?告诉WPF“现在停止搜索datatemplate并使用默认模板”?像将
Style
设置为
{x:Null}
一样,可以将任何控件的外观还原为默认外观。我没有其他方法可以实现这一点(当然,根据我的知识)。