Share via

How to disable an item of a data bound Listbox

Richard Jian 41 Reputation points
Apr 24, 2022, 5:34 PM

Hi, Geniuses,

I tried to disable an item randomly in a Listbox with XML data bound to it. It is built without any problem but collapses when clicking on the button of DisableItem2.

Here's the link: https://1drv.ms/u/s!Ag7LlPZhmWxci2IT3qxxq_XuezyS?e=ESpCsC

Thanks

MainWindow.xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfXmlDataBinding.MainWindow"
Title="MainWindow" Height="450" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="StudentsDataSource" Source="/WpfXmlDataBinding;component/Students.xml" d:IsDataSource="True"/>
<DataTemplate x:Key="StudentTemplate">
<StackPanel>
<TextBlock Text="{Binding XPath=Name}" Height="16" Width="103" TextAlignment="Left" />
</StackPanel>
</DataTemplate>
</Window.Resources>

 <Grid ScrollViewer.VerticalScrollBarVisibility="Disabled" Margin="0,0,-24.6,-46">

     <ListBox  x:Name="listbox_1" HorizontalAlignment="Left" Margin="52,53,0,0" VerticalAlignment="Top" Width="157" Height="141">
            <ListBoxItem>Fred</ListBoxItem>
            <ListBoxItem>Joe</ListBoxItem>
            <ListBoxItem>Mandy</ListBoxItem>
            <ListBoxItem>Bill</ListBoxItem>
            <ListBoxItem>Frank</ListBoxItem>
            <ListBoxItem>John</ListBoxItem>
            <ListBoxItem>Jen</ListBoxItem>
        </ListBox>
 <Button x:Name="DisableItem1" Content="Disable Item1" HorizontalAlignment="Left" Margin="52,223,0,0" VerticalAlignment="Top" Width="100" IsCancel="True" Click="DisableItem1_Click" RenderTransformOrigin="1.547,5.5"/>
        <Button x:Name="enableItem1" Content="Enable Item1" HorizontalAlignment="Left" Margin="52,255,0,0" VerticalAlignment="Top" Width="100" IsCancel="True" Click="EnableItem1_Click" RenderTransformOrigin="1.547,5.5"/>     

  <ListBox x:Name="listbox_2" 
 DataContext="{Binding Source={StaticResource StudentsDataSource}}"
 ItemTemplate="{DynamicResource StudentTemplate}" 
 ItemsSource="{Binding XPath=/Students/Student}"
 HorizontalAlignment="Left" Margin="300,53,0,0" VerticalAlignment="Top" Width="157" Height="141"  Background="White" Foreground="Black" BorderThickness="1" BorderBrush="#FFABADB3" >
 <ListBox.ItemsPanel>
 <ItemsPanelTemplate>
 <StackPanel Orientation="Vertical"/>
 </ItemsPanelTemplate>
 </ListBox.ItemsPanel>
 </ListBox>
 <Button x:Name="DisableItem2" Content="Disable Item2" HorizontalAlignment="Left" Margin="300,223,0,0" VerticalAlignment="Top" Width="100" IsCancel="True" Click="DisableItem2_Click"/>


    </Grid>
</Window>

MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfXmlDataBinding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

 Random rnd = new Random();
 int i = 0;

        private void DisableItem1_Click(object sender, RoutedEventArgs e)
        {
 i = rnd.Next(7);
            ListBoxItem _listItem = (ListBoxItem)listbox_1.Items[i];
            _listItem.IsEnabled = false;
        }

 private void EnableItem1_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem _listItem = (ListBoxItem)listbox_1.Items[i];
            _listItem.IsEnabled = true;
        }

 private void DisableItem2_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem _listItem = (ListBoxItem)listbox_2.Items[i];
            _listItem.IsEnabled = false;
        }
    }
}

Students.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Students>
<Student>
<Name>Fred</Name>
<Age>16</Age>
<Image>10</Image>
</Student>

<Student>
<Name>Joe</Name>
<Age>17</Age>
<Image>10</Image>
</Student>

<Student>
<Name>Mandy</Name>
<Age>17</Age>
<Image>10</Image>
</Student>

<Student>
<Name>Bill</Name>
<Age>16</Age>
<Image>10</Image>
</Student>

<Student>
<Name>Frank</Name>
<Age>17</Age>
<Image>10</Image>
</Student>

<Student>
<Name>John</Name>
<Age>17</Age>
<Image>10</Image>
</Student>

<Student>
<Name>Jen</Name>
<Age>17</Age>
<Image>10</Image>
</Student>

</Students>

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
835 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,651 Reputation points Microsoft External Staff
    Apr 25, 2022, 3:05 AM

    You could use the following code.

    private void DisableItem2_Click(object sender, RoutedEventArgs e)  
        {  
          i = rnd.Next(7);  
          ListBoxItem _listItem = (ListBoxItem)listbox_2.ItemContainerGenerator.ContainerFromItem(listbox_2.Items[i]);  
          _listItem.IsEnabled = false;  
        }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.