简体   繁体   English

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

[英]How to highlight a LongListSelector Item on the UI programmatically

I need to highlight and manipulate an item of LongListSelector on the UI for the user. 我需要在用户界面上突出显示和操作LongListSelector项。 I see (this) example on code samples but couldn't understand it well. 我在代码示例上看到了(此)示例,但并不太了解。

How can I change background of the inner StackPanel which belongs to the SelectedItem and add a TextBlock into it, programmatically in code behind? 我该如何更改属于SelectedItem的内部StackPanel背景,并以编程方式在后面的代码中向其中添加TextBlock

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

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

To make the sample you linked work with a StackPanel 为了使您链接的示例与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);
      }
    }
  } 
}

I am not sure whether I understood your question correctly since I am new in windows phone development. 由于我是Windows Phone开发的新手,因此我不确定我是否正确理解了您的问题。 Here is the code for changing the background of stackpanel and adding textblock to it programmatically. 这是用于更改堆栈面板背景并以编程方式向其添加文本块的代码。

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

    }

Best, B 最好,B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM