如何以编程方式在UI上突出显示LongListSelector项

时间:2013-07-30 00:12:49

标签: c# windows-phone-7 xaml windows-phone-8

我需要在UI上为用户突出显示和操作LongListSelector项。 我在代码示例上看到(this)示例,但无法理解它。

如何更改属于StackPanel的内部SelectedItem的背景,并在代码后面以编程方式添加TextBlock

<phone:LongListSelector.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">

        </StackPanel>
    </DataTemplate>
</phone:LongListSelector.ItemTemplate>

2 个答案:

答案 0 :(得分:1)

使您链接的样本与StackPanel

一起使用
private void lls_SelectionChanged(object sender, SelectionChangedEventArgs e) {
  var spList = new List<StackPanel>();
  GetItemsRecursive<StackPanel>(lls, ref spList);

  // Selected. 
  if (e.AddedItems.Count > 0 && e.AddedItems[0] != null) {
    foreach (var sp in spList) {
      if (e.AddedItems[0].Equals(sp.DataContext)) {
        sp.Background = new SolidColorBrush(Colors.Green);
        sp.Children.Add(new TextBlock { Text = "Hello" });
      }
    }
  }

  // Unselected. 
  if (e.RemovedItems.Count > 0 && e.RemovedItems[0] != null) {
    foreach (var sp in spList) {
      if (e.RemovedItems[0].Equals(sp.DataContext)) {
        sp.Background = (SolidColorBrush)Resources["PhoneBackgroundBrush"];
        sp.Children.RemoveAt(sp.Children.Count - 1);
      }
    }
  } 
}

答案 1 :(得分:0)

我不确定我是否理解你的问题,因为我是Windows手机开发的新手。这是用于更改stackpanel背景并以编程方式向其添加textblock的代码。

enter code here

    // Constructor
    public MainPage()
    {
        InitializeComponent();
        // change the background of stackpanel
        StackPanel st = new StackPanel();
        SolidColorBrush mysolidbrush = new SolidColorBrush();
        mysolidbrush.Color = Color.FromArgb(255, 100,100,10); // RGB color
        st.Background = mysolidbrush;

        // Adding textblock to the stackpanel 
        TextBlock txtblk = new TextBlock();
        st.Children.Add(txtblk);

    }

最佳, 乙