stigzler
Filtering an Observable Collection in WPF MVVM via ICollectionView
I have an observabelCollection of a data object:
Each
GistViewModel
has the following:
In terms of what this represents, each Gist has a collection of GistFiles. I am mapping this onto a TreeViewControl (root being Gists, Child being GistFiles) via the following xaml (abridged):
This works successfully. Now I am trying to introduce a filter feature. I have followed a guide online somewhere which advises using ICollectionView
. I implement this thus:
And changing my xaml to:
I'm just trying to get the default list presented first. However, this does not work. No list items are displayed.
GistFilesToFirstFilenameConverter
doesn't get called when Binding = GistsView
The full code is here: https://github.com/stigzler/VisGist/blob/master/VisGist/ToolWindows/MainWindow.xaml
What am I missing?3 replies
✅ Force Binding update in ViewModel where UpdateSourceTrigger==LostFocus
Hi folks - sorry to keep posting, but WPF is proving a difficult mare to break in....
I have a text editor control whose text is bound to a ViewModel Property and
UpdateSourceTrigger
set to LostFocus
It's not possible to set it to PropertyChanged
because it compares the text against a default text set which can be really long. If set to PropertyChanged, it introduces too much lag. Relevant code:
View:
ViewModel:
GistFileViewModel :
The issue is, I have a manual Save button. If I click this, the text control does not seem to loose focus and thus the bound property does not get updated. I'm trying to avoid View code behind as much as possible. The Save button click calls a Command in the ViewModel. How can I go about manually updating either the Property, or forcing the text control to lose focus?
Tried a few bit scraped from the interweb, but no joy.2 replies
✅ How to use IRelayCommand in CommunityToolkit.Mvvm with parameters?
I'm just learning WPF and also the community toolkit. I can get RelayCommands working OK if they're parameter-less, but struggling to implement a command bound to a Toggle Button to toggle a bool property on a databound control. My working approach to simple commands:
However, I have tried something similar for the ToggleButton:
The button and control xaml:
However, this renders the button inoperable (no rollover highlighting, command doesn't execute) - it works without the Command Binding.
Any ideas?
EDIT: I've just realised that I don't technically need a Command here, I can just bind the ToggleButton's IsChecked property to the property directly. However, I'm still intersted to know why the above broke the ToggleButton and how to send parameters via cmmands using the Community Toolkit.
77 replies
✅ WPF - Accessing a TreeViewItem's root UserControl DataContext for a Command
I am trying to execute a Command which exists in the ViewModel bound to the root parent UserControl of a child TreeViewItem. This is from a ContextMenu bound to root TreeViewItems. The TreeView code:
Key code:
Broader context:
https://paste.mod.gg/khruicdqkfhy/1
MainWindowViewModel
is the DataContext of MainWindow, within which the Command I'm targeting exists. I've tried various permutations of RelativeSource for Binding, as despite xaml intellisense suggesting the binding is right by offering the command as an option, it doesn't fire on selecting the relevant ContextMenuItem.
Any ideas?2 replies
WPF - how to incorporate existing code into a Content Control
Hi. I'm at the limits of my WPF-Fu. I have code monkeyed some xmal that can re-orientate two sides with a separator (think winforms spliterPanel) to be either horizontally or vertically aligned depending on a ViewModel property.
The problem is now coming when I'm trying to fit my existing code into this lifted code. As I say - I'm out my depth as it's now getting into the realms of ContentControls and DataTemplates (nested I think at that). COuld someone please help out?
Code re: the above here: https://paste.mod.gg/yveppqhunols/0
2 replies
WPF Treeview HierarchicalDataTemplate on Type with sub-collection
Hi Folks. I have the following two models (abridged for brevity) and mainViewModel:
and my xaml:
I'm only getting the Gist displayed and not the GistFiles on a sub-level. I know I'm getting this wrapped around my head conceptually - what am I doing wrong?
5 replies
✅ WPF - how to approach a treeview displaying different types at different levels via MVVM
I'm just learning WPF - so seeking some design advice. I'm designing a Visual Studio extension to view and edit Gists. Each type Gist has a collection of type GistFiles (Files property in a Gist):
I'll have 3 views inside the main View: GistsBrowser (user control=treeview), GistEditor (user control) and GistFileEditor (user control). I'm thinking I need to store the Collection of Gists in the MainViewModel, as different UserControls/ViewModels will need access to a shared GistsCollection to ensure synchronicity.
For the GistsBrowser, I need to make a specific treeview control to display all Gists at the root level which can be expanded to show all of the GistFiles within a Gist. There will be no further child levels.
Thus, this UserControl will need its own ViewModel (GistsBrowserViewModel). However, I'm stuck on a few things:
How to structure the Gists collection in MainViewModel. Not sure if I need to map the Github data objects onto my own models, or map them directly onto a viewmodel. Do I need a Gist/GistFile viewmodel? Also, this links with:
What data construct to use for the Gists collection and how to hook it up to a TreeView (given level 0 will be Gists and level 1 GistFiles)
I hope that makes some kind of sense. Any pointers for further reading/research would be appreciated.
109 replies
✅ WPF - set UIElement (image) in XAML depending on databound Property Enum value
I am trying to dynamically change a StatusBar image via a view's XAML depending upon a databound ViewModel Property. The offender in question:
The enum:
Pseudocode I'm trying to achieve:
Could soemone give me a steer, please?
6 replies
✅ WPF - how to correctly call a method in the ViewModel when an event occurs in the View?
I want to call a method in the ViewModel when an event happens in the View. That is, when the View is loaded, I want it to execute a Authentication method in the ViewModel:
I get
Error CS0407 'Task MainWindow.MyToolWindow_LoadedAsync(object, RoutedEventArgs)' has the wrong return type
at compile time.When coding MyToolWindow_LoadedAsync, I followed the intellisence warning about naming, uing void and not awaiting to end up with how it looks preently.
I know it's me - what am I doing wrong?15 replies
WPF - Resource Dictionary - "the resource could not be resolved"
I'm working on a VS extension. This is a WPF UserControl, thus no app.xaml. I'm trying to move my styles out of the main control's body to seprarte resource dictionaries. I've followed guides, but getting an error on any elements whic previously worked when they were in
<UserControl.Resources>
In the user control:
and:
(this throws the error - working before removal). And the resource dict:
What am I doing wrong?2 replies
✅ Shorten Property code
Is there a simpler way to write this:
I know you can do:
And also:
But with the former, you can't do the
mainWindowControl.SaveButton.BorderBrush =...
and with the latter, you can't set GistEdited (as it'd result in an infinite loop)6 replies
Casting types inline
I'm still not clear on all the routes to this, and which one is best for readability. I'm trying to get
.Count
from a child object of object sender
. The Type hierarchy is:
Here are a couple of ways I've tried:
Of course things could be nested deeper. Which is the best way (realise 'best' is ambiguous)8 replies