C# 如何在Windows Phone的列表框项目中查找组件?

C# 如何在Windows Phone的列表框项目中查找组件?,c#,windows-phone,C#,Windows Phone,我需要从列表框项目中检索组件。不是从点击或选择更改的事件。有没有一个简单的方法来实现这一点? 如果我没记错的话,在安卓系统中,你可以写: layoutRoot.findViewById(R.id.name); 在Windows Phone上有类似的方法吗 更新:以下是我迄今为止尝试过的,但不起作用: 当我有ListBoxItem时,Daniela在选项5中所说的似乎起作用。因此,例如,当我在ListBoxItem上有一个点击事件时,这可以正常工作: private void ListBoxI

我需要从列表框项目中检索组件。不是从点击或选择更改的事件。有没有一个简单的方法来实现这一点? 如果我没记错的话,在安卓系统中,你可以写:

layoutRoot.findViewById(R.id.name);
在Windows Phone上有类似的方法吗


更新:以下是我迄今为止尝试过的,但不起作用:

当我有ListBoxItem时,Daniela在选项5中所说的似乎起作用。因此,例如,当我在ListBoxItem上有一个点击事件时,这可以正常工作:

private void ListBoxItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    ListBoxItem item = sender as ListBoxItem;
    // I can now retrieve a component inside the ListBoxItem
    TextBox textBox = item.FindName("myTextBox") as TextBox;
}
但我希望在不从ListBoxItem触发任何事件时执行此操作。 JoonasL说的话看起来像是我可以用的东西,但我无法让它发挥作用

// will not compile ( Cannot implicitly convert type 'object' to... )
ListBoxItem item = x.Items[0];

// if cast the value will be null
ListBoxItem item = x.Items[0] as ListBoxItem;

// will return the object used to populate that ListBoxItem, not the actual ListBoxItem.
// item will have a ItemViewModel object.
List<ItemViewModel> list = ....
this.myListBox.ItemsSource = list;
var item = x.Items[0]
//将不会编译(无法将类型“object”隐式转换为…)
ListBoxItem=x.Items[0];
//如果强制转换,该值将为空
ListBoxItem=x.Items[0]作为ListBoxItem;
//将返回用于填充该ListBoxItem的对象,而不是实际的ListBoxItem。
//项将有一个ItemViewModel对象。
列表=。。。。
this.myListBox.ItemsSource=列表;
var item=x.Items[0]
在Google上搜索时,我发现了一些可以用来查找ListBoxItem中的组件的东西,但我认为应该有一种更简单的方法。在这里,他们使用了一种新的方法

或者您还想要别的吗?

编辑: 在lisbox中添加一个名称(x:name=“something”) 将所有项目强制转换为列表,或选择一个项目并将该项目强制转换为正确的类型。例如:

    private void Button_Click(object sender, RoutedEventArgs e)
{
  var list = yourListbox.Items;
  var itemCastAsCorrectObjectInstance = (ItemViewModel)list.FirstOrDefault();
  textblock.Text = itemCastAsCorrectObjectInstance.LineOne;
}
ItemViewModel是我们创建的一个类,ItemViewModels列表用作listbox的itemssource

有几种方法可以做到这一点(有些示例是WPF,但代码非常类似,这只是给您一个大概的想法)

  • 在代码中创建listbox并检索项,如my JoonasL提供的示例所示

        private void GetUserRecommendations()
    {
        var obj = _helper.GetList<Recommendations>(@"http://localhost:1613/Home/GetAllRecommendations");
    
        _items.Clear();
    
        foreach (var item in obj)
        {
            _items.Add(item);
        }
    
        itemListView.ItemsSource = _items;
    }
    
  • 为列表框提供一个名称,并通过引用代码中的名称来访问项目

    var item=itemListView.SelectedItem作为SomeClass

  • 通过绑定到另一个元素(仅限XAML)访问选定项


    RadDataBoundListBox有一个名为ItemTap的事件(不确定是否为普通ListBox),但我的解决方案是这样的:

    private void FocusTextBox(object sender, ListBoxItemTapEventArgs e)
    {           
                    txtBox = e.Item.FindChildByType<TextBox>();
    
                    var item = itemListView.SelectedItem as PanoramaApp1.//Your Selected Item Type Here;
    
                    //Do Something With item Here
    
                    itemListView.SelectedItem = null;
    }
    
    private void FocusTextBox(对象发送方,ListBoxItemTapEventArgs e)
    {           
    txtBox=e.Item.FindChildByType();
    var item=itemListView.SelectedItem作为PanoramaApp1//您在此处选择的项目类型;
    //对这里的物品做点什么
    itemListView.SelectedItem=null;
    }
    
    如果我有一个对ListBoxItem的引用,选项5实际上是有效的,例如:
    ListBoxItem.FindName(“myTextBlock”)
    但在不从ListBox触发任何事件时,如何访问ListBoxItem。这是有效的:
    private void ListBoxItem_Tap(object sender,System.Windows.Input.GestureEventArgs e){ListBoxItem=sender as ListBoxItem;var textBox=item.FindName(“myTextBox”);}
    JoonasL说的不起作用:
    ListBox x=new ListBox();ListBoxItem=x.Items[0]
    x.Items[0]返回用于填充ListBox的对象,而不是acutal ListBoxItem
    x.Items[0]
    将只返回用于填充该ListBoxItem的对象,而不是实际的ListBoxItemiixi JoonasL正确-该示例显示如何获取第一个项目-及其组件。如果需要填充listboxitem的内容,则必须将该项强制转换为正确的类型。C#是一种强类型语言,我们喜欢类型;)请你标出正确的答案,这样其他人也可以得到帮助吗?@IrisClasson如果没有解决我的问题,我就不能把任何答案标注为正确
        void ItemView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var itemProperty = ((ListBoxItem) e.ClickedItem).SomeProperty;
    }
    
    <Border Margin="10" BorderBrush="Silver" BorderThickness="3" Padding="8">
    <DockPanel>
      <TextBlock>Choose a Color:</TextBlock>
      <ComboBox Name="myComboBox" SelectedIndex="0">
        <ComboBoxItem>Green</ComboBoxItem>
        <ComboBoxItem>Blue</ComboBoxItem>
        <ComboBoxItem>Red</ComboBoxItem>
      </ComboBox>
      <Canvas>
        <Canvas.Background>
          <Binding ElementName="myComboBox" Path="SelectedItem.Content"/>
        </Canvas.Background>
      </Canvas>
    </DockPanel>
    
        private void Somepage_Loaded(object sender, RoutedEventArgs e)
    {
        if (SomeCondition())
        {
            var children = (sender as Panel).Children;
            var child = (from Control child in children
                     where child.Name == "NameTextBox"
                     select child).First();
            child.Focus();
        }
    
    private void FocusTextBox(object sender, ListBoxItemTapEventArgs e)
    {           
                    txtBox = e.Item.FindChildByType<TextBox>();
    
                    var item = itemListView.SelectedItem as PanoramaApp1.//Your Selected Item Type Here;
    
                    //Do Something With item Here
    
                    itemListView.SelectedItem = null;
    }