【问题标题】:change databound listbox to longlistselector将数据绑定列表框更改为 longlistselector
【发布时间】:2013-10-02 00:57:53
【问题描述】:

我有一个列表框,可以通过以下代码打开和查看哪些项目...

    private void AccountsList_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
            var listBoxItem = AccountsList.ItemContainerGenerator.ContainerFromIndex(AccountsList.SelectedIndex) as ListBoxItem;
            var txtBlk = FindVisualChildByType<TextBlock>(listBoxItem, "txtBlkAccountName");
            xCa = txtBlk.Text;

            NavigationService.Navigate(new Uri(string.Format("/ViewAccount.xaml?parameter={0}&action={1}", a.ToString(), "View"), UriKind.Relative));
    }

&

    T FindVisualChildByType<T>(DependencyObject element, String name) where T : class
    {
        if (element is T && (element as FrameworkElement).Name == name)
        {
            return element as T;
        }

        int childcount = VisualTreeHelper.GetChildrenCount(element);

        for (int i = 0; i < childcount; i++)
        {
            T childElement = FindVisualChildByType<T>(VisualTreeHelper.GetChild(element, i), name);
            if (childElement != null)
            {
                return childElement;
            }
        }
        return null;
    }

现在我正在实现 longlistselector 而不是列表框。 长列表选择器显示数据库中的所有项目,但我在打开此列表中的项目时遇到问题...我无法在此 longlistselector 中使用 SelectedIndex 请帮助...

【问题讨论】:

    标签: c# windows-phone-7 longlistselector


    【解决方案1】:

    我建议您更改工作流程。不听 Tap 事件,而是听 SelectionChanged 事件。从此事件中,您可以获得 SelectedItem。 SelectItem 是 Items 绑定到的对象。

    示例:您的 ItemsSource 是 ListBox 或 LongListSelector 中的每个项目都绑定到 MyObject 的一个实例。您的“txtBlkAccountName”TextBlock 应该将其 Text 绑定到 MyObject 类的 AccountNumber 属性。

    private void LongListSelector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var myObj = AccountsList.SelectedItem as MyObject;
        if(myObj == null) return;
    
        var accountNum = myObj.AccountNumber;
        NavigationService.Navigate(new Uri(string.Format("/ViewAccount.xaml?parameter={0}&action={1}", accountNum, "View"), UriKind.Relative));
    
        // set the selectedItem to null so the page can be navigated to again 
        // If the user taps the same item
        AccountsList.SelectedItem = null;
    }
    

    【讨论】:

      【解决方案2】:

      要让项目被点击,请将 Tap 放在 ItemTemplate 而不是列表中,然后您可以使用 sender 属性来检索您想要的值。
      此外,您应该能够使用 DataContext 来检索您想要的任何内容,而不是使用 FindVisualChildByType 来获取您想要的值:

      private void AccountsItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
      {
              FrameworkElement element=sender as FrameworkElement ;
              Account item= element.DataContext as Account ;
      
              xCa = item.Name;
      
              NavigationService.Navigate(new Uri(string.Format("/ViewAccount.xaml?parameter={0}&action={1}", a.ToString(), "View"), UriKind.Relative));
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-14
        • 2013-04-06
        • 2010-10-20
        • 2017-02-01
        • 1970-01-01
        • 2014-08-06
        • 2011-07-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多