0

カスタム コントロールについて学んでおり、例として autoCompleteTextBox を作成しています。WPF プロジェクト (v4.5 with vb.net 4.5) のカスタム コントロールを作成しており、テキスト ボックスの基本クラスを使用しています。次に、ポップアップ、リストボックス、およびボタンをコントロールに追加しました。リストボックスのデータ テンプレートのカスタム コントロールに依存関係プロパティがありますが、データ テンプレートをリスト ボックスに pssed できません。

データテンプルの依存関係プロパティは次のとおりです。

#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は次のとおりです。

<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 を渡すのを手伝ってくれる人はいますか。

前もって感謝します

4

1 に答える 1

0

わかりました、アイテムテンプレートを次のように割り当てる場所を変更したところ、機能するようになりました:

#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
于 2013-04-20T21:00:45.477 に答える