none
how to make item in listbox not selectable

    Question

  • Hi,

    I've a listbox with few items and textblock for a title.

    something like:

    <ListBox>

    <TextBlock Text="BLA BLA"/>

    <ListBoxItem Content="Item 1" />

    </ListBox>

    how can i make the textblock to be non selectable?

    Thanks,

    Gidi.

    Wednesday, February 29, 2012 3:05 PM

Answers

  • I usually use the ItemContainer for this:

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:vm="clr-namespace:WpfApplication74"
    		 x:Class="WpfApplication74.MainWindow"
            Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication74">
    
        <Grid>
    		<ListBox x:Name="list1" ItemTemplate="{DynamicResource DataTemplate1}" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}">
    			<ListBox.Resources>
    				<DataTemplate x:Key="DataTemplate1">
    					<Grid>
    						<TextBlock Text="{Binding Name}"/>
    					</Grid>
    				</DataTemplate>
    				<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
    					<Setter Property="IsHitTestVisible" Value="{Binding IsSelectable}"/>
    				</Style>
    			</ListBox.Resources>
    		</ListBox>
    
        </Grid>
    </Window>
    

    using System.Windows;
    using System.Collections.Generic;
    
    namespace WpfApplication74
    {
        public class Person
        {
            public string Name { get; set; }
            public bool IsSelectable { get; set; }
        }
       
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                list1.ItemsSource = new List<Person> { new Person { Name = "Pete", IsSelectable = true }, new Person { Name = "Sally", IsSelectable = false } };
            }
        }
    }

    Regards,
    Pete


    #PEJL

    • Marked as answer by gidi Wednesday, February 29, 2012 3:56 PM
    Wednesday, February 29, 2012 3:19 PM
    Moderator
  • Oh, just noticed your actual issue is you are using the listBox completely wrong. The two items you are placing inside the ListBox are both being treated as individual ListBox Items.

    The correct WPF solution is you need to template your ListBox Items as shown in my example above.

    Regards,
    Pete


    #PEJL

    • Marked as answer by gidi Wednesday, February 29, 2012 3:56 PM
    Wednesday, February 29, 2012 3:22 PM
    Moderator

All replies

  • You can disable it if you mean you don't want them to select the text.

    <TextBlock Text="BLA BLA" IsEnabled="False"/>



    If you mean you don't want to be able to select the listboxitem then you can disable the item.

            <ListBox>
                <ListBoxItem IsEnabled="False">
                    <TextBlock Text="BLA BLA" />               
                </ListBoxItem> 
                <ListBoxItem Content="Item 1" />
            </ListBox>
    In which case the disabled item will be greyed - so you would also have to style the listbox if that's not the effect you want.
    • Edited by Andy ONeill Wednesday, February 29, 2012 3:22 PM
    Wednesday, February 29, 2012 3:15 PM
  • I usually use the ItemContainer for this:

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:vm="clr-namespace:WpfApplication74"
    		 x:Class="WpfApplication74.MainWindow"
            Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication74">
    
        <Grid>
    		<ListBox x:Name="list1" ItemTemplate="{DynamicResource DataTemplate1}" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}">
    			<ListBox.Resources>
    				<DataTemplate x:Key="DataTemplate1">
    					<Grid>
    						<TextBlock Text="{Binding Name}"/>
    					</Grid>
    				</DataTemplate>
    				<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
    					<Setter Property="IsHitTestVisible" Value="{Binding IsSelectable}"/>
    				</Style>
    			</ListBox.Resources>
    		</ListBox>
    
        </Grid>
    </Window>
    

    using System.Windows;
    using System.Collections.Generic;
    
    namespace WpfApplication74
    {
        public class Person
        {
            public string Name { get; set; }
            public bool IsSelectable { get; set; }
        }
       
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                list1.ItemsSource = new List<Person> { new Person { Name = "Pete", IsSelectable = true }, new Person { Name = "Sally", IsSelectable = false } };
            }
        }
    }

    Regards,
    Pete


    #PEJL

    • Marked as answer by gidi Wednesday, February 29, 2012 3:56 PM
    Wednesday, February 29, 2012 3:19 PM
    Moderator
  • Oh, just noticed your actual issue is you are using the listBox completely wrong. The two items you are placing inside the ListBox are both being treated as individual ListBox Items.

    The correct WPF solution is you need to template your ListBox Items as shown in my example above.

    Regards,
    Pete


    #PEJL

    • Marked as answer by gidi Wednesday, February 29, 2012 3:56 PM
    Wednesday, February 29, 2012 3:22 PM
    Moderator