<ListBox Name="ListBox_Test" Height="200">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="StackPanel_Template">
<TextBlock Text="{Binding Path = Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
2 . Code C# để lấy TextBlock
ListBoxItem item = (ListBoxItem )ListBox_Test.ItemContainerGenerator.ContainerFromIndex(ListBox_Test.SelectedIndex);
if (item == null)
return;
DataTemplate data = item.ContentTemplate;
UIElement content = VisualTreeHelper.GetChild(item, 0) as UIElement;
ContentPresenter presenter = (ContentPresenter)(VisualTreeHelper.GetChild(content, 0));
if (presenter == null)
return;
StackPanel obj = data.FindName("StackPanel_Template", presenter) as StackPanel;
Tham khảo : từ anh Khánhtl - FalconSoftware
No comments:
Post a Comment