| Perfil de PaulioIT BytesBlogListasScraps | Ajuda |
|
30 de abril How to remove items from a bound list box or how to avoid "Operation not supported on read-only collection."Today I got a rude error message from my application when I attempted to do a very simple operation, that of removing an item from ListBox; "Operation not supported on read-only collection." The problem stems from the fact that I’d bound my ListBox to an ObservableCollection, once bound the Items collection becomes read-only. A know some others have had the same problem so I thought I’d blog about my solution. Utilizing a function, I’d previously blogged about, to find the ListBoxItem from a button click I then do a little casting and the item is removed; private void buttonDelete_Click(object sender, RoutedEventArgs e) { ListBoxItem selectedItem = FindAssociatedListBoxItem(sender);
// if we have found an item then remove it if (selectedItem != null) { MyColl myColl = this.ListBoxProducts.ItemsSource as MyColl; MyObject selectedObj = selectedItem.DataContext as MyObject; myColl.Remove(selectedObj); } } 22 de abril WebDD 09 - a quick blog Last Sat' I attended WebDD 09, here is my brief summary; What's New In Silverlight 3? - Mike Taulty Mike is a good speaker and knows his Silverlight onions so I thought this one would be worth attending even if I have read about the majority of the changes. Overall it was a good session and it's good to see live demos of the changes. One of the demo's failed but Mike was determined to provide an answer why and was forthcoming a couple of couple of hours later (and on his blog). What's good in .NET 4 and Visual Studio 2010 - Alex Mackey The idea behind this session was to preview the, "new stuff". However, I came away from this session a little disappointed. I was hoping for some insights but what I got was really a summary from publicly available blogs and CTPs. Still it was ok and it's quite a nice idea for someone to collate these remours into one place but to paraphrase Alex, 'I've not looked deeply at X because it's likely to change' so perhaps this session is too early to watch too? I want it on that one, that one and that one! And it all needs to be synched! - Andrew Westgarth Andrew presented the deployments options in II7. I was interested to see the Deploy utility, that seems to be an implementation of a Powershell host, and I especially like the idea of easily keeping production and test sites in sync. However, that was really the interesting part of the session and the rest felt like it was just repeating itself. I think this was probably due to the fact that almost all the demos failed and probably took all the momentum out of the session. An Introduction to jQuery - Andy Gibson I took a chance on this one since I have been dabbling with jQuery so I should be beyond an introduction now but I thought I might learn something I'd missed. Overall the session was a decent introduction to jQuery, although since I understand it you'd really have to ask someone who was new to jQuery, and it was reassuring to hear some things I'd guessed at rather than knew for certain. For a WebDD I would have liked to have seen an intermediate/advance session rather than a introduction. ASP.NET MVC best practices - Sebastien Lambla Sebastien provided a witty and interesting session on what was wrong with the out-of-the-box MVC framework and how his teams worked around them. I really liked this session even if the demo gods caused a blue-screen and other demo failures. I just felt he could have quickly introduced the Windsor framework as I could see a number of people were confused by its sudden use but that's only a tiny point. Suffice to say that I've added his blog to my reader ASP.NET 4.0 - Mike Ormond Mike is a another MS session stalwart and presented a good session on ASP.NET 4. Although all the nice things about ASP.NET Ajax WPF style binding was good to see I felt like shouting hallelujah at the control provided over Client IDs, something I've been campaigning for on forums. For me ASP.NET is a terrible framework but I concede that v4 looks to finally have started to consider professional developers so maybe I'll be enticed back...maybe. 21 de abril Parallels Desktop 4 - disappointing About a month ago I decided to upgrade my Parallels Desktop 3 to 4. I needed to do a fair amount of Windows Development work and 4 was hailed as providing large increases in performances and this, together with a years subscription to anti-virus, was good enough reason to upgrade. Oh dear, unfortunately disk space is a rare on my laptop so I didn't backup my v3 disk and just upgraded to 4 feeling sure I would have no need to go back. 4 is so disappointing; 1. Vista performance - everything about using Vista feels sticky and glitchy whereas before it was absolutely fine. 2. Application performance - launching applications feels slow. Everything seems to run as if there is no concurrency. Launch two applications and you may as read a book before you get any sensible response. V3 was great. 3. Switching back to OSX - this was great in V3, a quick ALT-CMD-ENTER and I was out of full-screen Windows and back to OSX in pretty much an instant. In V4 I struggle to even get back to OSX let alone in a speedy fashion. Overall I'd give V4 2/10 since it does run the applications but it's pale impression of its older brother. Oh dear, oh dear. [Edit] - Hopefully I may have found the problem. In the VM configuration is a 'Show Advanced' option, selecting this provided access to the CPU options. For some reason it was set to use only one core from my dual CPU. Selecting this seems to have improved things, I'll update this blog entry should it prove to fix the above problems. 13 de abril How to use a button within a List ItemOne of the problems with Silverlight is that the templating mechanism is really good but you can quickly get a bit stuck. I ran into one such example when I had created a nice DataTemplate for a ListBoxItem that contained a button. When the button is clicked I wanted a specific action to take place in the context of the containing list box item, i.e. I wanted the list box item to be selected because the user has pressed a button within the item. <DataTemplate x:Key="MyDataTemplate"> <Grid> <Button Content="Edit" Click="Button_Click" /> </Grid> </DataTemplate>
The problem is that the Button_Click event will fire but none of the event args will explain which of the potentially many buttons has been pressed. WPF uses tricks such as binding the button’s tag to an ancestor element but SL2 doesn’t (I believe) offer this feature. One solution is to skip the button click event and simulate the button via some mouse down events and event bubbling but that seemed too dirty for me. So I thought I could walk the control tree and find which ListBoxItem the button was within. You have to be a bit careful here because templating/themes can introduce a different logical tree so you must walk the tree looking for a ListBoxItem rather than assuming a specific number of hops. BTW, in theory the ListBoxItem itself may have been “styled” out too so you should degrade gracefully if you can’t find the ListBoxItem. So I wrote some code to loop and walk the FrameworkElement.Parent properties. This didn’t work since the .Parent returned null at the ContentPresenter boundary, i.e. before the ListBoxItem. Still not sure exactly why that is, but I needed a quick solution so I turned to the Visual tree instead. This proved to be the solution… private void Button_Click(object sender, RoutedEventArgs e) {
Button buttonSender = sender as Button; // which item is containing the button? DependencyObject currentElement = buttonSender; ListBoxItem selectedItem = currentElement as ListBoxItem; DependencyObject theParent = VisualTreeHelper.GetParent(currentElement); while (selectedItem == null && theParent != null) { currentElement = theParent; selectedItem = theParent as ListBoxItem; theParent = VisualTreeHelper.GetParent(currentElement); }
// if we have found an item then ensure it is selected if (selectedItem != null) { selectedItem.IsSelected = true; }
}
Thanks to Keith Mohoney's Post for the information about the Visual Tree Helper |
|
|