如何在DataTemplate中的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>
答案 0 :(得分:0)
acLastName.SelectedItem
将是您的UserDetails对象。
因此,通过以下方式投射该小狗并访问UserDetailsId:
((UserDetails)acLastName.SelectedItem).UserDetailsId