【问题标题】:How to Find the object of The ListItem in ListBox in Windows Phone7如何在 Windows Phone 7 中的 ListBox 中查找列表项的对象
【发布时间】:2011-12-21 11:22:33
【问题描述】:

我有一个 ListBox,我在这里绑定 IList Like (Cities)。

我想要一个像 OnItemDataBound 在 Windows Phone7 中的 .NET 中的事件。

就像每个城市都被绑定时一样(如果有 10 个城市,则此事件将触发 10 次)此事件将触发,因此我必须对此事件进行更多计算。在这种情况下,我必须找到绑定到 ListItem 的对象,以便我可以进行一些计算。 WP7 中是否有类似 .NET 中的 OnItemDataBound 的事件。

<ListBox Loaded="lstFlights_Loaded" Height="635" x:Name="lstFlights"   
                         ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border CornerRadius="10" Margin="10">
                                <Border.Background>
                                    <ImageBrush ImageSource="../Images/rowbg.png"></ImageBrush>
                                </Border.Background>
//Some data here
     </Border>

                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

我正在绑定如下数据:

lstFlights.ItemsSource = objCities;

在绑定每个城市时,我想要一个事件来根据绑定到 ListItem 的城市填充一些列表项(例如:我想更改文本块文本等)。为此,我需要一个类似于 WP7 中的 OnItemDataBound 的事件。我有如下列表选择器:

在 SelectionChanged 事件中,我也想更改列表项。

还有一件事 IList(objCities) 来自服务,所以我无法更改该对象 因此,如果我想更改列表框中的任何 TextBlock,我必须执行 FindName,并且必须为每个绑定的城市分配计算值。

【问题讨论】:

  • 为什么不能在绑定到列表之前进行计算?
  • 我从服务中得到的响应非常复杂,所以我想在绑定后进行计算。而且我有一个列表选择器,在选择更改时我也必须更改 ListBox 的计算,这就是我想要 OnItemDataBound 事件的原因。请问有没有这样的活动。
  • 你能展示一下你现在拥有的东西吗?我不知道 OnItemDataBound 事件,但对于选择更改,您可以使用 selectionchanged 事件来处理必须对用户选择进行计算的情况。
  • 感谢 ChristiaanV 的回复。我已经编辑了我的问题,请查看一次,然后告诉我如何在此处继续。

标签: windows-phone-7


【解决方案1】:

ObservableCollection 集合中有一个 CollectionChanged 事件,用于添加/删除新元素。

ListBox 提供了一些访问ListBoxItem 的方法。您可以在list.ItemContainerGenerator 类中看到它们。

您可以一起使用它来达到所需的效果。

有一个简短的例子展示了如何在某些条件下(甚至或不)为新添加的项目制作红色前景:

ObservableCollection<string> items = new ObservableCollection<string>();

    public MainPage()
    {
        InitializeComponent();

        items.CollectionChanged += (s, e) =>
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (e.NewItems != null)
                {
                    foreach (var item in e.NewItems)
                    {
                        ListBoxItem listitem = (ListBoxItem)list.ItemContainerGenerator.ContainerFromItem(item);
                        if (DateTime.Parse(listitem.Content.ToString()).Second % 2 == 0)
                        {
                            listitem.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
                        }
                    }
                }
            });
        };
        list.ItemsSource = items;            
    }

    private void AddButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        items.Add(DateTime.Now.ToLongTimeString());
    }

    private void RemoveButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Random rnd = new Random();
        if (items.Count > 0)
        {
            int index = rnd.Next(0, items.Count);
            items.RemoveAt(index);
        }
    }
}

【讨论】:

  • 感谢 Ku6opr 的回复。但我的应用程序中没有任何 ObservableCollection。我有一个 IList,我正在使用 ItemsSource 绑定列表框。在这里我想要一个像 OnItemDataBound 这样的事件。
【解决方案2】:

改用ListBoxItemContainerGeneratorItemsChanged 事件怎么样?

this.myListBox.ItemContainerGenerator.ItemsChanged += new System.Windows.Controls.Primitives.ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged);

void ItemContainerGenerator_ItemsChanged(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs e)
{
    if (e.Action == NotifyCollectionChangedAction.Add) 
    ...

【讨论】:

  • 感谢鑫的回复。但是在这里我怎样才能找到我的 ListItems,例如我在 Listbox 中有一个 TextBlock 用于该文本框我必须绑定价格,所以为此我将进行一些计算,我会得到价格,但是我如何在这个事件中找到 TextBlock .在 .net 中,我们将在 OnItemDataBound 事件中执行以下操作:RepeaterItem item = (RepeaterItem)e.Item;城市 city= (City)item.DataItem;字面量 litPrice = (字面量)item.FindControl("litPrice");
  • 在 ItemsChanged 事件中:DependencyObject listitem = list.ItemContainerGenerator.ContainerFromIndex(e.Position.Index + e.Position.Offset); DependencyObject child = VisualTreeHelper.GetChild(listitem, 0); 等等...
  • 感谢 Ku6opr 的回复。但我无法使用 DependencyObject listitem = list.ItemContainerGenerator.ContainerFromIndex(e.Position.Index + e.Position.Offset); 找到 listitem我得到的列表项为空。
  • 试试这个,var item = (ListBoxItem)myListBox.ItemContainerGenerator.ContainerFromIndex(myListBox.Items.Count - 1);
  • Xin:我也尝试了上面的代码,但仍然 listitem 我得到了 null。
猜你喜欢
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
相关资源
最近更新 更多