获取AutoCompleteBox数据模板中的TextBlock值

本文关键字:TextBlock AutoCompleteBox 数据 获取 | 更新日期: 2023-09-27 18:04:48

我如何在一个数据模板内得到一个WPF AutoCompleteBox容器内的TextBlock的值?

下面是我的AutoCompleteBox XAML
<my:AutoCompleteBox  Name="acLastName"
    FilterMode="StartsWith"
    SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
    SelectionChanged='acLastName_SelectionChanged'
    ValueMemberPath="LastName">
        <my:AutoCompleteBox.ItemTemplate>
            <DataTemplate x:Name='UserDetails'>
                <StackPanel Orientation="Horizontal">
                    <TextBlock x:Name='UserId' Text="{Binding UserDetailsId}"/>
                    <TextBlock Text="{Binding LastName}" />
                    <TextBlock Text="{Binding FirstName}" />
                    <TextBlock Text="{Binding UserId}" />
                    <TextBlock Text="{Binding Comapany}" />
                </StackPanel>
           </DataTemplate>
        </my:AutoCompleteBox.ItemTemplate>
</my:AutoCompleteBox>

获取AutoCompleteBox数据模板中的TextBlock值

acLastName.SelectedItem将是您的UserDetails对象。

因此将这个小块转换并通过它访问UserDetailsId:

((UserDetails)acLastName.SelectedItem).UserDetailsId