Is there a way to have the selected items of a ListBox as a ListBoxItem in the event SelectionChanged (on the ListBox) in WPF?
Right now, when I call listBox.SelectedItems I get the list of items in the format of my data source.
For example, when I create my ListBox I bind it to a CustomListBoxViewModel as a data source, so when I called SelectedItems I get a list of CustomListBoxViewModel objects.
Thanks!
EDIT:
The code that was given in the chosen answer worked perfectly for my use case.
On the other hand, I completely understand that this is a violation of the MVVM pattern. I'll use the code you provided in code behind of a xaml view.
The reason behind my original question was that I need to implement a ListBox that has some disabled ListBoxItems inside. Those items would have to be unselectable.
I tried to use an Attached Property IsSelectable on my ListBoxItems and it didn't work well. This attached property was greatly inspired by this post. The problem was that when I was directly selecting a disabled item, the attached property worked perfectly. The item couldn't be selected and it wasn't inside the SelectedItems collection. But if I wanted to select all the items by pressing CTRL+A, all the ListBoxItems were selected even the disabled ones and were found in the SelectedItems collection.
Whatever reason you're doing this for, it's probably a serious violation of MVVM that you'll bitterly regret for the rest of your life.
But the first step on the road to perdition is always an easy one.
Here's how:
var listBox = (ListBox)sender;
var selectedListBoxItems =
listBox.SelectedItems.Cast<Object>()
.Select(item => (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromItem(item))
.ToList();
Just don't come crying to me when it all ends in tears.
No but seriously, there's are a few good reasons to do this kind of thing in WPF, but they're far less common than somebody new to WPF would expect. Pretty much any normal case is best handled by having your C# code interacting only with your data items, and do stuff to the ListBoxItems in XAML via styles and templating. Once you get used to that way of thinking, it's very powerful, flexible, productive, and maintainable. Codebehind is what you do for odd cases when all other reasonable avenues fail. Drag and drop, for example.
Related
I have a screen with a TextBox in which the user can type a 2-character state code. Below the TextBox is a ListBox containing all 50 state codes. The TextBox is bound to property in the VM, and the SelectedItem is bound to a property in the VM. That all works fine.
The way I want the UI to work is when the user selects a state from the ListBox, the TextBox is automatically filled in, and this works fine.
Where it gets messy is when the user types in the state in the TextBox. When I get the first character, what I want to do is reposition the list box at the first matching state code for that letter, so for instance, if the ListBox is sitting at "AK" (Alaska) and the user is going to type "ID" for Idaho, when I get the "I" I want to position the ListBox so you can see the first "I" state, which is "IA" (Iowa).
If I use code behind and point SelectionChanged=BringSelectionIntoView with this method coded as follows, it works great:
private void BringSelectionIntoView(object sender, SelectionChangedEventArgs e)
{
ListBox lb = (ListBox)sender;
lb.ScrollIntoView(lb.SelectedItem);
}
All I have to do is scan the list of state codes until the first letter matches, then update the Index property to which SelectedIndex is bound, and poof, the BringSelectionIntoView() method gets invoked and I have exactly the UI behavior I want.
Trying to do this in a purest MVVM methodology, however, has proved quite frustrating. I'm not using MVVMLight or ExpressionBlend--I want a simple way to do this in MVVM. I understand the purest's mindset of not putting any UI code in the view, but the framework is insanely cumbersome to enact this kind of behavior. There's a point of diminishing returns when you have to create such obtuse plumbing to force yourself to adhere to a pattern when it's far more practical to put in the method with 2 lines of code that works perfectly.
So my question is this: am I doing something wrong and is there a simple way to make this work without violating MVVM? It's disappointing if the solution requires additional SDKs or someone's framework. That would suggest that MVVM doesn't have particularly good legs to stand on in a generic OOP sense.
Does someone see an error in what I'm trying to do, or do you see a simplistic solution here? Thanks!
MVVM is not about not having any code behind.
What you're talking about here is VIEW behavior. Which fits perfectly in the code behind, as long as you're not messing with the DATA in the event handlers.
You're using a VIEW event handler to manipulate a VIEW aspect.
That doesn't break MVVM.
Keep it that way. Keep it Simple.
You should still have a ViewModel and a Model to hold the DATA that the UI shows.
This is a perfect use case for an attached behavior. You can write this behavior once and use it for all listboxes without ever having to write any additional code. If you would like me to elaborate, ask and I'll post more information.
I'm pretty new to c#, the first thing that I'm trying to make is a ListView with data bindings which has turned out ok.
I'm now trying to make items have a twist button if the underlying model has any children (like the TreeView). Each of the children will have columns the same as all the top level items.
How would I go about doing this? Is there an already existing control like this? If not would I be better off dressing up a TreeView to look like a ListView, or dress up a ListView to look like a TreeView?
I went down the road outlined in this solution which dresses up a TreeView, but the end result looks pretty awful and the heading is actually just an item, so you lose all the nice column sizing and column buttons that can hook up to column sorting that you get in ListView so that route actually seems like it would be more work.
I noticed the new task manager has a control exactly like what I'm trying to create, I don't know how this made? probably in C though.
Microsoft provides a sample that appears to be what you are looking for. A write-up of the example can be found here:
http://msdn.microsoft.com/en-us/library/vstudio/ms771523(v=vs.90).aspx
When you build and run the example you will end up with something resembling this:
There is a large amount of templating done in the example, so you will be able to make things look the way you want.
What you describe sounds a bit like a TreeListView, and if you google 'WPF TreeListView' you will see some solutions that might be good for you. I have used one from Telerik, but it might be overkill depending on how complicated your needs are.
If you only want one sub-level like the image you attached, you might want to just roll your own using a ListView with a complex DataTemplate for the first column which would show an expander button and a simple ListBox bound to the children items.
Similar to the answer here, except your cell would have a checkbox styled to look like the arrow, the text for the item, and a child ListBox. Then bind the visibility of the child ListBox to the state of the checkbox.
I'm trying to find out exactly what happens when the data bound with ItemsSource changes. So which method should I override to be able to make the items animate to their new positions in the sorted list?
From what I found out so far I might have to hijack the LayoutUpdated of the ScrollViewer inside the ListBox? How do I find out what happens there in order to be able to attach an animation to the ListBoxItems' position assignments?
Actually it would be even nicer to do this with some attached triggers and behaviors and not having to extend the ListBox class.
Edit: as it turned out both PagedCollectionView and CollectionViewSource destroys and recreates the item list on sorting, so rearranging the list with an animation doesn't seem to be possible without either implementing sorting manually by moving existing items around in the list or to create some sort of item buffer, both of which sounding like a possible source of weird bugs and performance bottleneck. If anyone has a working solution to these though I'd be glad to get any pointers!
In good old (well!!) WinForms days the datagrids row used to the be the actual control and you could then access the DataItem.
In WPF its all flipped and dataGrid.Items is just the source data.
I am probably doing this the wrong way round as im a bit of a WPF newb but how can I iterate through the rows of my gridview grabbing the values from certain labels, textboxes etc?
Yes, you are doing this the wrong way round. What you should be doing is iterating through the items in your data source - it's where all the values are, after all.
It's possible to iterate through the WPF objects, but it's not trivial. And there's a significant problem that you'll run into if you try.
You can use the VisualTreeHelper class to search the visual tree and find the DataGrid's descendant objects. If you play with this long enough, eventually you'll figure out how to find the specific controls you're looking for. But the DataGrid (actually, the VirtualizingStackPanel in its control template) virtualizes its visual children. If an item hasn't appeared on the screen yet, its WPF objects haven't been created yet, and you won't find them in the visual tree. You may not be able to find what you're looking for, not because you don't have a way of finding it, but because it doesn't exist.
If you're using value converters and formatting in your bindings (which is the only reason I can think of that you'd want to look at the WPF objects and not the underlying data items), I'm afraid the answer is: don't do that. Do the value conversion and formatting in your data source, and expose the results as properties that can be bound to directly.
It's certainly possible to use WPF without using the MVVM pattern. But this is the kind of brick wall that you can run into if you don't.
You can use this
public DataGridRow TryFindRow(object item, DataGrid grid)
{
// Does not de-virtualize cells
DataGridRow row = (DataGridRow)(grid as ItemsControl).ItemContainerGenerator.ContainerFromItem(item);
return row;
}
where item represent the data displayed on the row.
Hope this helps.
I'm certain this has come up before, but I haven't been able to find the answer.
I made a basic ViewModel that contains a list of People (an array of Person) with a property called SelectedPerson, which naturally points to the currently selected Person in the list of People. I also have a ListBox and a TreeView that are databound to the ViewModel's People list.
What I'd like to do is to keep the ListBox's SelectedValue and TreeView's SelectedItem in sync with with the ViewModel's SelectedPerson. The idea is that no matter how the SelectedPerson is modified (through a control, through code, etc), all the controls should update themselves properly. I can get it to work with two ListBoxes, which is nice, but I can't get it to work with a ListBox and a TreeView because the TreeView's SelectedItem is readonly and apparently unavailable through XAML.
Where should I look to get ideas on making this work?
Also note that I'm trying to make this work in pure XAML. No code-behind as XAML files in my application can be loaded and changed dynamically.
Thanks!
You can Use Selector.IsSyncronizedWithCurrentItem.
You can bind both thye listbox and treeview to the same datasource and make sure that the IsSyncronized parameter is set to true. Then any changes to the current item in one will be reflected in the other.
More information can be found here:
link text
I asked around and the best solution I could find was here
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cc73893a-3383-4328-a002-ed8fb002a19d
It works for me but it's not the most optimal solution at this point.