Solved

How do I select a listbox item by its tag in WPF and then disable it.

Posted on 2008-10-28
6
2,113 Views
Last Modified: 2013-11-12
Hello Everyone,

I am trying to select a listbox item by its tag value and then disabling it. I have no clue on how to this wiht a WPF control. Can someone show me an example on how to achieve this?
Thanks,

Stephane
0
Comment
Question by:MrTouya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22830389
I found some sample code which may help.

private:
   void listBox1_SelectedIndexChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Get the currently selected item in the ListBox.
      String^ curItem = listBox1->SelectedItem->ToString();

      // Find the string in ListBox2.
      int index = listBox2->FindString( curItem );

      // If the item was not found in ListBox 2 display a message box,
      //  otherwise select it in ListBox2.
      if ( index == -1 )
            MessageBox::Show( "Item is not available in ListBox2" );
      else
            listBox2->SetSelected( index, true );
   }
0
 

Author Comment

by:MrTouya
ID: 22833866
This is what I have to do now. Is there a better way of doing the same?
 

private void MenuButtonState(string MenuButtonName, string State)
{
ListBoxItem CurrentSelectedItem = (ListBoxItem)this.MenuButtons.SelectedItem;
ListBoxItem SelectionToChangeStateOf = (ListBoxItem)this.MenuButtons.FindName(MenuButtonName);
ItemCollection MenuItems = this.MenuButtons.Items;
int IndexToDisable = MenuItems.IndexOf((ListBoxItem)SelectionToChangeStateOf);
int CurrentSelectedIndex = MenuItems.IndexOf((ListBoxItem)CurrentSelectedItem);
MenuButtons.SelectedItem = IndexToDisable;
if (State == "Disabled")
SelectionToChangeStateOf.IsEnabled = false;
else SelectionToChangeStateOf.IsEnabled = true;
MenuButtons.SelectedItem = CurrentSelectedItem;
}
 
Thanks,
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22843946
Does it work?

>>>> SelectionToChangeStateOf = (ListBoxItem)this.MenuButtons.FindName(MenuButtonName);

Does the FindName return a (reference to a) ListBoxItem or does it return an index? In the latter case you hardly could cast an integer to a ListBoxItem.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MrTouya
ID: 22846853
It Works. I have tried other ways, but this is the best way that I have come up with that works.
 
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 22851066
>>>> It Works.

That is good. I personally have only little experience with managed C++ and WPF but I would try simpler code like


private void MenuButtonState(string MenuButtonName, string State)
{
    ListBoxItem SelectionToChangeStateOf = (ListBoxItem)this.MenuButtons.FindName(MenuButtonName);
    if (State == "Disabled")
         SelectionToChangeStateOf.IsEnabled = false;
    else
         SelectionToChangeStateOf.IsEnabled = true;
}

If your code above was working that one should work as well, right?
0
 

Author Comment

by:MrTouya
ID: 22855251
Yep. That is much more elegant.

Thanks,
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
windbg taking it's time 32 108
Issues with C++ Class 19 119
Mobile apps - web/native or hybrid? 1 120
Creating a Windows C++ program in visual studio 2015 5 29
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question