C
C#2y ago
br4kejet

❔ WPF Binding TreeViewItem name to string, and falling back to another data context property

I made a little file explorer app and I wanted to try and allow folders to have no names; The data context contains a property string called Name and I bound the string to a text block in my TreeViewItem's style which works fine. But in the event that a folder's name is null or empty, i want it to default to something like Folder of ? items, where ? would be how many items it contains (i'm using an observable collection to store children items in the data context). I tried using a converter and binding to the name, but i have no way of passing the data context to the converter to access the data context. Is there a way around this?
5 Replies
br4kejet
br4kejet2y ago
this is the text block I have currently, but i wanna somehow pass the data context to the converter
<TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NameConverter}}" Margin="5 0 0 0"/>
<TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NameConverter}}" Margin="5 0 0 0"/>
br4kejet
br4kejet2y ago
and then this is the converter code that would run
br4kejet
br4kejet2y ago
I might just add a string to my data context called "FolderNameFinal" or something, which gets modifed when a child is added/removed or the name changes
sibber
sibber2y ago
don't use a converter for that just make another property
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.