Answered by:
How can I do this binding in ListView / ListViewITem?

Question
-
I have the following in my WPF ListView... my ListView is using objects of type
NameDescLVItem (my own class)...
GridViewColumn
GridViewColumn.CellTemplate
DataTemplate
Rectangle
<some other stuff at the same level>
Now I want to bind Rectangle.Visibility... to do so I need ListViewItem.IsKeyboardFocused || (ListViewItem.Selected && "IsItemBeingEdited")
Now, I'm not sure how to do this because the expression is more complicated then a single property... I thought about adding a new property to the NameDescLVItem class and binding to that, but it doesn't seem possible to bind to properties on NameDescLVItem... I tried doing this:
Visibility
="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:NameDescLVItem}}, Path=IsKeyboardFocused, Converter={StaticResource SelectedItemToVisibilityConverter}}">
But at runtime, I get an exception saying it can't find an ancestor of that type.
Can't really derive a class from ListView and add the property there because then I don't know what item WPF data binding is asking about.
Any ideas?
Thursday, April 1, 2010 12:38 AM
Answers
-
Sounds like you might need to combine some DataTriggers with MultiBindings, but it's hard to say without the rest of the XAML.
http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx
http://msdn.microsoft.com/en-us/library/system.windows.datatrigger.aspx
A quick way of Binding to a control, instead of using RelativeSource, is just to give the control an x:Name property, and your binding could go...
{Binding ElementName="listview", Path=SelectedItem} (or whatever property of the ListView you want to bind to)
Hope that helps,
Matt
Thursday, April 1, 2010 12:59 AM
All replies
-
Sounds like you might need to combine some DataTriggers with MultiBindings, but it's hard to say without the rest of the XAML.
http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx
http://msdn.microsoft.com/en-us/library/system.windows.datatrigger.aspx
A quick way of Binding to a control, instead of using RelativeSource, is just to give the control an x:Name property, and your binding could go...
{Binding ElementName="listview", Path=SelectedItem} (or whatever property of the ListView you want to bind to)
Hope that helps,
Matt
Thursday, April 1, 2010 12:59 AM -
Well, yeah, thats the thing... I can't bind to SelectedItem because thats not the item I'm interested in. Lets say I have 10 items, 0 through 9. The WPF runtime is asking about items 0 through 9, not the selected item.
Your multibinding suggestion may do the trick though, cuz then I can bind to multiple unrelated things.
I'll give that a whirl tommorow.
I think if I bind to ListViewItem.IsKeyboardFocused, ListViewItem.IsSelected and ListView.IsKeyboardFocusedWithin and then run it through a multi converter I'll be able to get it working...
Thanks for the suggestion.
Thursday, April 1, 2010 2:37 AM