我需要从用户控件中访问列表框中选定的项目。我该如何管理?
例如,如果我在MainPage.xaml中有ListBox x:Name="NewPicturesListBox"
!--Panorama item New one-->
<controls:PanoramaItem Header="New one" Name="Pan1" >
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Margin="0,0,-12,0" x:Name="NewPicturesListBox"
我需要从DetailControl.xaml.cs访问它们 下面的代码可以在MainPage.xaml中给我结果,如果有可能存在其他页面呢?
if (NewPicturesListBox.ItemContainerGenerator != null)
{
var contextMenuListItem = (ListBoxItem)(NewPicturesListBox.ItemContainerGenerator.ContainerFromItem(((MenuItem)sender).DataContext));
if (contextMenuListItem != null)
{
var selectedPicture = (contextMenuListItem).Content as Picture;
答案 0 :(得分:0)
你可以拥有ViewModel和Property,让我们调用它来保存selectedValue的selectedPicture,然后从你的XAML绑定它:
<ComboBox Name="NewPicturesListBox" MinWidth="100" VerticalAlignment="Center"
ItemsSource="{Binding ListOfPictures}"
DisplayMemberPath="Name"
SelectedValue ="{Binding Path=SelectedPicture}" Width="141" />