locked
Highlighting List Item RRS feed

  • Question

  • Hi,

     How to highlight a list item. I have written the listbox using datatemplate for displaying image along with the textblock and I have binded those values to a list of custom type. Here is the code,

    XAML

      <ListBox Margin="0,0,-23,8" Grid.Row="3" x:Name="lstImages">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Width="16" Height="16" Source="{Binding ImageName}"/>
                                <TextBlock Text="{Binding FileName}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
     MyImage.cs

    public class MyImage

    {

    public string ImageName { get; set; }public string Text { get; set; }

    }

     

    CodeBehind.cs

    private void PopulateImageList()

    {

    List<MyImage> imageList = new List<MyImage>();

    Uri uri;

    foreach (string item in imageFileNames)

    {

    uri =
    new Uri(App.Current.Host.Source, "Icons/" + item);imageList.Add(new MyImage { ImageName = uri.AbsoluteUri, Text = item.ToString() });

    }

    lstImages.ItemsSource = imageList;

    }

     

    Please help me

     
    Tuesday, May 12, 2009 1:44 AM

Answers

  • Hi,

    Thanks for you reply. I tried using the following method and it is working fine for me,

     

     private void HighlightImage(DependencyObject element,string strImageName)
            {
                StackPanel stkpnl = element as StackPanel;
                if (stkpnl != null)
                {
                    if (stkpnl.Name == "stkItems")
                    {
                        TextBlock nameBlock = VisualTreeHelper.GetChild(stkpnl, 1) as TextBlock;
                        if (nameBlock.Text == strImageName)
                        {
                            stkpnl.Background = new SolidColorBrush(Colors.Yellow);
                        }
                        else
                            stkpnl.Background = new SolidColorBrush(Colors.Transparent);//clear the previously marked item
                    }
                }
    
                int children = VisualTreeHelper.GetChildrenCount(element);
                for (int i = 0; i < children; i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(element, i);
                    HighlightImage(child,strImageName);
                }
            }
     

     

    Tuesday, May 12, 2009 2:44 AM

All replies

  • Hi,

    Why don't you add a Border around the StackPanel and use its property to highlight the Item.

    (If I misunderstood you then let me know otherwise mark reply as answer if applicable.) 

    -Vinit     

    Tuesday, May 12, 2009 1:52 AM
  • Hi,

    Thanks for you reply. I tried using the following method and it is working fine for me,

     

     private void HighlightImage(DependencyObject element,string strImageName)
            {
                StackPanel stkpnl = element as StackPanel;
                if (stkpnl != null)
                {
                    if (stkpnl.Name == "stkItems")
                    {
                        TextBlock nameBlock = VisualTreeHelper.GetChild(stkpnl, 1) as TextBlock;
                        if (nameBlock.Text == strImageName)
                        {
                            stkpnl.Background = new SolidColorBrush(Colors.Yellow);
                        }
                        else
                            stkpnl.Background = new SolidColorBrush(Colors.Transparent);//clear the previously marked item
                    }
                }
    
                int children = VisualTreeHelper.GetChildrenCount(element);
                for (int i = 0; i < children; i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(element, i);
                    HighlightImage(child,strImageName);
                }
            }
     

     

    Tuesday, May 12, 2009 2:44 AM