2013-07-30 46 views
0

我需要突出顯示和操作用戶界面上的LongListSelector項目。 我在代碼示例中看到(this)示例,但無法很好地理解它。如何以編程方式突出顯示用戶界面上的LongListSelector項目

如何更改屬於SelectedItem的內部StackPanel的背景,並在代碼後面以編程方式添加TextBlock

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

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

回答

0

我不知道我是否正確理解你的問題,因爲我是windows phone開發的新手。這是用於更改堆疊面板的背景並以編程方式向其添加文本塊的代碼。

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

    } 

最佳, 乙

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); 
     } 
    } 
    } 
} 
樣本
相關問題