2012-06-30 2 views
-3

목록 상자 항목에서 구성 요소를 검색해야합니다. 탭 또는 선택 변경 이벤트가 아닙니다. 이것을 달성 할 수있는 간단한 방법이 있습니까? 나는 올바른 기억한다면 , 안드로이드에 그냥 쓸 수 있습니다 : 윈도우 폰에서이 작업을 수행하는 유사한 방법Windows Phone에서 목록 상자 항목의 구성 요소를 찾는 방법은 무엇입니까?

layoutRoot.findViewById(R.id.name); 

있습니까?


업데이트 : 여기에 내가 지금까지 뭘하려하지만 작동하지 않습니다 다니엘라 옵션 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의에서 이벤트를 트리거하지 않을 경우이 작업을 수행 할 수 : 나는 예를 들어 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] 

Google에서 검색 할 때 ListBoxItem 내부의 구성 요소를 찾는 데 사용할 수있는 것이 있지만 더 쉬운 방법이라고 생각합니다. 여기서는 VisualTreeHelper을 사용합니다.

+0

당신은 정답을 표시시겠습니까 다른 사람들도 도움을받을 수 있습니까? –

+0

@IrisClasson 내 문제를 해결하지 못했지만 대답을 올바르게 표시 할 수 없습니다. – iixi

답변

0
ListBox x = new ListBox(); 
ListBoxItem item = x.Items[0]; 

또는 다른 것을 원하십니까?

+0

'x.Items [0]'은 실제 ListBoxItem이 아닌 해당 ListBoxItem을 채우는 데 사용 된 객체 만 반환합니다. – iixi

+0

iixi JoonasL이 맞습니다. 예제에서는 첫 번째 항목과 그 구성 요소를 가져 오는 방법을 보여줍니다. listboxitem을 채우려는 항목을 원하면 항목을 올바른 유형으로 캐스팅해야합니다. 나는 ListBoxItem의에 대한 참조, 예를 들어이있는 경우 –

3

편집 : 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의 목록이 목록 상자에 대한 itemssource로 사용된다.

Here is an app example I've made for you

은을 (예제 중 일부는 WPF를하지만 코드가 당신에게 일반적인 생각주고 그냥 매우 유사하다)

  1. 코드의 목록 상자 만들기를 할 수있는 방법은 여러가지 방법이 있습니다 제공되는 예에서와 같이 및

    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; 
    } 
    
  2. 가 바인딩 변경된 일정에 선택한 항목 (또는 다른 이벤트를 검색 내 JoonasL 항목을 검색 목록 상자)

    void ItemView_ItemClick(object sender, ItemClickEventArgs e) 
    { 
        var itemProperty = ((ListBoxItem) e.ClickedItem).SomeProperty; 
    } 
    
  3. 은 목록 상자에 이름을 입력하고 코드

    VAR 항목 = itemListView에 이름을 다스 려하여 항목에 액세스 할 수 있습니다.SomeClass로서 SelectedItem;

  4. 액세스 다른 소자에 결합함으로써 선택 항목 (XAML 만)

    <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> 
    

  5. 이 LayoutRoot에게

VAR의 myTextBlock = (TextBlock의) this.FindName 검색 ("myTextBlock");

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(); 
    } 
+0

옵션 5 실제로 작동), C#을, 우리는 유형처럼 강력한 형식의 언어입니다 : 'listBoxItem.FindName ("myTextBlock");' 을하지만 내가 어떻게 액세스합니까 ListBox에서 이벤트를 트리거하지 않을 때 ListBoxItem. 이것은 작동합니다 : 'private void ListBoxItem_Tap (Object sender, System.Windows.Input.GestureEventArgs e) { ListBoxItem item = sender as ListBoxItem; var textBox = item.FindName ("myTextBox"); }' @ JoonasL은 효과가 없다고 말했습니다 : ListBox x = new ListBox(); ListBoxItem item = x.Items [0]; ' x.Items [0]은 ListBox를 채우는 데 사용되는 개체를 반환하고 acutal ListBoxItem은 반환하지 않습니다 – iixi

0

RadDataBoundListBox이 ItemTap (일반 목록 상자에 대해 확실하지)라는 이벤트를 가지고 있지만 내 솔루션은이 같은 갔다 :

아니면 무언가 같이

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; 
} 
관련 문제