开发者

How to change state of container items based on container state (WPF/XAML)

开发者 https://www.devze.com 2023-01-07 02:06 出处:网络
I have a Listbox with items created through databinding.The item template creates a custom view for each generated item.Each generated item view is its own user control.

I have a Listbox with items created through databinding. The item template creates a custom view for each generated item. Each generated item view is its own user control.

I'd like to change the state of the listbox to something like "Details" vs. "Compact" and have each item have its own state changed automatically. The view item knows what to show or hide based on its state. I can't seem to change the state of that generated user control from a higher level (the button for changing view modes).

I hope this is clear enough. I'll add details i开发者_如何学编程f it helps.

Thank you very much for any help!


Set a data trigger in your generated control which is bound to the relative property of a parent. More info here on uses of RelativeSource - your looking for the ancestor option, which will be the parent listbox you mention.

Lets say you expose a ViewType property on the listbox, then something like

{Binding Path=ViewType, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}

should work.


You can set

MyValue = "{Binding Path=SelectedItem.CustomProperty, Converter=CustomPropertyConverter}"

This will walk down the object chain down your selected item.

FindAncestor climbs up the chain.

0

精彩评论

暂无评论...
验证码 换一张
取 消