none
关于创建CustomControl,中定义的DataTemplate的默认值应该如何设置? RRS feed

  • 问题

  • 我创建一个CustomControl,在里面定义了一个HeaderTemplate,它的类型是DataTemplate。在为它设置默认值的时候,只要一设置Null,在页面上创建控件的时候不设置,程序就会奔溃。然后我就尝试吧默认值改成New DataTemplate(),程序就不会奔溃了,但是这会引起其他问题。所以我想问一下DataTemplate的默认值应该如何设置?

            public DataTemplate HeaderContentTemplate
            {
                get { return (DataTemplate)GetValue(HeaderContentTemplateProperty); }
                set { SetValue(HeaderContentTemplateProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for HeaderContentTemplate.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty HeaderContentTemplateProperty =
                DependencyProperty.Register("HeaderContentTemplate", typeof(DataTemplate), typeof(CustomContentControl), new PropertyMetadata(null));

    2014年8月1日 10:06

答案

  • 你好 Nomooo,

    据我所知,默认的DataTemplat都是仅包含一个Grid。

            <DataTemplate x:Key="DataTemplate1">
                <Grid/>
            </DataTemplate>

    --James

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年8月4日 4:28
    版主