Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:50:39+00:00 2026-06-15T05:50:39+00:00

Some of the items in my ListBox use a template that contains a button

  • 0

Some of the items in my ListBox use a template that contains a button and a TextBox. How can I make it such that it is impossible to select these items from the list, but still possible to interract with the button?

EDIT:

I still need to be able to select other items in this list, just not ones with this template.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:50:41+00:00Added an answer on June 15, 2026 at 5:50 am

    We can use a attached property to the ListBoxItem (after I implemented I found someone who had done almost the same) :

    public class ListBoxItemEx
    {
        public static bool GetCanSelect(DependencyObject obj)
        {
            return (bool)obj.GetValue(CanSelectProperty);
        }
        public static void SetCanSelect(DependencyObject obj, bool value)
        {
            obj.SetValue(CanSelectProperty, value);
        }
        public static readonly DependencyProperty CanSelectProperty =
            DependencyProperty.RegisterAttached("CanSelect", typeof(bool), typeof(ListBoxItemEx), new UIPropertyMetadata(true, OnCanSelectChanged));
    
        private static void OnCanSelectChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var item = sender as ListBoxItem;
            if (item == null)
                return;
    
            if ((bool)args.NewValue)
            {
                item.Selected -= ListBoxItemSelected;
            }
            else
            {
                item.Selected += ListBoxItemSelected;
                item.IsSelected = false;
            }
        }
    
        private static void ListBoxItemSelected(object sender, RoutedEventArgs e)
        {
            var item = sender as ListBoxItem;
            if (item == null)
                return;
            item.IsSelected = false;
        }
    }
    

    The xaml:

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Width="525"
        Height="350">
    <Window.Resources>
        <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="local:ListBoxItemEx.CanSelect" Value="{Binding CanSelect}"/>
        </Style>
    </Window.Resources>     
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Grid Name="LayoutRoot">
        <ListBox ItemsSource="{Binding Elements}" ItemContainerStyle="{DynamicResource ListBoxItemStyle}" SelectionMode="Multiple" />
    </Grid>
    

    The viewmodel:

    public class ViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged values
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    
    
        public List<Dummy> Elements { get; set; }
    
        public ViewModel()
        {
            this.Elements = new List<Dummy>(){
                new Dummy() { CanSelect =true, MyProperty = "Element1"},
                new Dummy() { CanSelect =false, MyProperty = "Element2"},
                new Dummy() { CanSelect =true, MyProperty = "Element3"},
                new Dummy() { CanSelect =false, MyProperty = "Element4"},
                new Dummy() { CanSelect =true, MyProperty = "Element5"},
                new Dummy() { CanSelect =true, MyProperty = "Element6"},
                new Dummy() { CanSelect =true, MyProperty = "Element7"},
                new Dummy() { CanSelect =true, MyProperty = "Element8"},
                new Dummy() { CanSelect =false, MyProperty = "Element9"},
            };
        }
    }
    
    public class Dummy
    {
        public bool CanSelect { get; set; }
    
        public string MyProperty { get; set; }
    
        public override string ToString()
        {
            return this.MyProperty;
        }
    }
    

    The only caveat with this approach is that trying to select an unselectable item unselects the current selected one if the ListBox has single selection, or unselects all, if the ListBox has extended selection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's a ListBox with some long items. These long items are getting beyond the
I have a ListBox that uses DataTemplateSelector to dynamically decide what template to use
well i have a listbox with some items inside. i want to detect a
A ListBox and a ContextMenu are created dynamicaly. The ListBox has some items. How
I have some items in my .emacs that I don't want to run if
I have some items called tickers which can be created and subscribed to. When
I have some items that I'd like the user to be able to filter
I have a foreach for editing some items. Each item has a Save button.
I'm changing the look of some Controls that I use in my Microsoft Surface
I have some items populated in the listbox from the database in my asp.net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.