WPF CustomControl通过datatemplate到列表框无法正常工作

时间:2013-04-19 06:27:31

标签: .net wpf custom-controls datatemplate dependency-properties

我正在学习自定义控件,并以autoCompleteTextBox为例。我正在为WPF项目创建自定义控件(v 4.5与vb.net 4.5)并且它正在使用文本框基类。然后我向控件添加了一个弹出窗口,列表框和按钮。我在列表框的datatemplate的自定义控件中有一个依赖项属性,但我无法将数据模板转换为列表框。

这是datatemple的依赖属性:

#Region "DEPENDENCY PROPERTIES -- ItemTemplate"
    Public Property ItemTemplate As DataTemplate
        Get
            Return GetValue(ItemTemplateProperty)
        End Get
        Set(ByVal value As DataTemplate)
            SetValue(ItemTemplateProperty, value)
        End Set
    End Property
    Public Shared ReadOnly ItemTemplateProperty As DependencyProperty = DependencyProperty.Register( _
                        "ItemTemplate", GetType(DataTemplate), GetType(AutoCompleteTextBox), _
                        New FrameworkPropertyMetadata(Nothing, FrameworkPropertyMetadataOptions.None, _
                        New PropertyChangedCallback(AddressOf OnItemTemplateChanged)))

    Shared Sub OnItemTemplateChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
        Dim actb As AutoCompleteTextBox = TryCast(d, AutoCompleteTextBox)
         If actb IsNot Nothing Then
            Dim TempTemplate As DataTemplate = TryCast(e.NewValue, DataTemplate)
            If TempTemplate IsNot Nothing Then
                actb.ItemTemplate = TempTemplate
            End If
        End If
     End Sub
#End Region

这是我的xaml,用于声明usercontrol的小文本:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:krisis="clr-namespace:Krisis.Controls;assembly=Krisis.Controls"
    Title="MainWindow" Height="350" Width="525" x:Name="MyWindow"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <DataTemplate x:Key="CollectionTemplate">
            <Border BorderBrush="Green" BorderThickness="2" CornerRadius="5" Padding="5,5,5,2">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="OBJECT:  "/>
                    <TextBlock Grid.Column="1" Text="{Binding Name}"/>
                    <TextBlock Grid.Row="1" Text="{Binding id}"/>
                    <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Job}"/>
                </Grid>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <krisis:AutoCompleteTextBox ItemsSource="{Binding Collection}" 
                                    ItemTemplate="{StaticResource CollectionTemplate}" 
                                    MaxmimumMatches="15" 
                                    MinimumFilterCharacters="1" 
                                    DisplayPath="Name"
                                    Width="497" MinHeight="35" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,41,10,243" />
    </Grid>
</Window>

我的问题:是当我使用它来声明我的itemtemplate时,它没有得到应用。列表框只显示列表框中每个对象的对象类型名称,而不是任何对象属性值。

有人可以帮我使用依赖项属性来传递自定义控件中列表框的DataTemplate。

提前致谢

1 个答案:

答案 0 :(得分:0)

好吧,我改变了将itemtemplate指定给以下内容的位置,现在可以正常工作:

#Region "APPLY TEMPLATE"
    Public Overrides Sub OnApplyTemplate()
        MyBase.OnApplyTemplate()

        '' if template is not nothing then initialize controls and wire up the event handlers
        If Me.Template IsNot Nothing Then

            InitializeListbox()
            If ResultsListBox IsNot Nothing Then
                OnItemSourceChanged(ItemsSource)
                AddHandler ResultsListBox.KeyDown, AddressOf ResultListBox_KeyDown
                AddHandler ResultsListBox.SelectionChanged, AddressOf ResultListBox_SelectionChanged
                ResultsListBox.ItemTemplate = ItemTemplate
                ResultsListBox.ItemsSource = ItemsSource
            End If
        End If
    End Sub
#End Region