Avalonia Tip: Binding to ListBoxItem.IsSelected from Inside a DataTemplate #18576
TomOSullivan
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
🧠 Avalonia: Show a Button Only When a
ListBoxItem
Is SelectedAvalonia doesn’t support
DataTriggers
insideDataTemplates
, which makes it less obvious how to change part of a list item’s UI based on selection state.Here’s how to make a button appear only when a
ListBoxItem
is selected.✅ The Setup
We’re binding to a simple
string[]
orObservableCollection<string>
. Each item has a label and a button. The button is only visible when its container is selected.🧩 How It Works
IsSelected
is a property on the container (ListBoxItem
), not the bound data.RelativeSource AncestorType=ListBoxItem
walks up the visual tree to get it.AncestorType
.👇 ViewModel for Context
🛠 Notes
TreeViewItem
,TabItem
, etc., too.Avalonia’s styling system is powerful, but sometimes under-documented. This is one of those tricks that’s worth locking in.
Beta Was this translation helpful? Give feedback.
All reactions