mario12136
mario12136
CC#
Created by mario12136 on 7/26/2023 in #help
Transition to Microsoft's Dependency Injection Library
https://gist.github.com/MarioFares/f935dd74aa461feefb227631af1c2b3e I was wondering what I can do to improve my App.xaml.cs file. I have seen this https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/ioc but one of my service implementations takes an argument and I am not sure how best to do that. Otherwise, I am not sure if I am doing anything wrong.
4 replies
CC#
Created by mario12136 on 7/25/2023 in #help
❔ When to separate controls into their own Views and Viewmodels?
While I'd like to think I understand MVVM, this is a question that I think is not really covered. It is quite easy to have a main view model, if your app has window, that is full of commands and properties, bound to menuitems and buttons. How do I know that a control perhaps deserves its own view and viewmodel. Technically, what I have for my application, I can see it go either way: (1) have everything in the main view model or (2) separate certain controls into their own viewmodels. How do I make that decision? Even if this question is a matter of preference, what is your preference and though process. Would it ever make sense to have the File menu have its own view model. How about a tight knit group of controls, or popups that only display data (maybe a lot of data)? If I do end up with multiple views that need view models in my main view, how best to provide these view models as data contexts if their contructors have params (DI)?
3 replies
CC#
Created by mario12136 on 7/23/2023 in #help
What control should I use?
I want to create a "feature search" command palette for my WPF application (like the one in Visual Studio). I have 1 main view model from which I want to serve commands using reflection to my command palette. I have tried making the command palette a wpf Window and have its own view model but I was wondering if there is some control that makes more sense and that would make my life easier. Some requirements: - I want the control to be hidden rather than having to close and restart it again - When I click outside its borders I want it to be hidden again - I want to be able to press Escape and it becomes hidden again - I want to execute a command AND hide the control. Doing this has proven tricky with a Window control I was also wondering if I should use an ItemsControl or a ListBox to show the list of available commands. I want as I am typing in the searchbox for the first item to be selected and if I press up/down I go to the next or previous item.
11 replies
CC#
Created by mario12136 on 7/22/2023 in #help
❔ Attributes for commands: implementing "feature search"
Background: So I have worked on a simple text editor app and have a MainViewModel full of commands that I bind to menu items. I wanted to see if I can create something like Feature Search in Visual Studio or think command palette like vscode if you will. That's when I stumbled upon this amazing concept called attributes. I want to see if this is a good way to achieve what I want. I create an attribute like this
[AttributeUsage(AttributeTargets.Property)]
public class CommandInfoAttribute : Attribute
{
public string Description { get; }

public CommandInfoAttribute(string description)
{
Description = description;
}
}
[AttributeUsage(AttributeTargets.Property)]
public class CommandInfoAttribute : Attribute
{
public string Description { get; }

public CommandInfoAttribute(string description)
{
Description = description;
}
}
and in my MainViewModel I add this to my commands:
[RelayCommand]
[property: CommandInfo("New File")]
private async Task NewFileAsync()
{
...
}
[RelayCommand]
[property: CommandInfo("New File")]
private async Task NewFileAsync()
{
...
}
and then, using reflection, I would find all public IRelayCommands defined in my mainviewmodel and get their descriptions along with any other attributes, for example what MenuItem they are defined for ("File > New File"). That data I can feed to my search control that will display a list of commands and once clicked used the .Execute() method of said command. Perhaps I can create a model that takes my MainViewModel and returns this command information (haven't thought this one completely through). I was wondering if this is this best or even a good way to go on about this. I wanted to achieve this with minimal changes to my existing codebase and this is what I could come up with.
4 replies
CC#
Created by mario12136 on 7/18/2023 in #help
❔ Smooth scrolling in WPF
I am working on a WPF app and was wondering if there is anyway to implement smoother scrolling than the default behavior. Scrolling like in notepad or other applications seems much more smoother and generally slower than scrolling in a default control in WPF. So I was wondering if there is perhaps a nuget package for this as I would like this to be used in all my app. Or if there is something specific for AvalonEdit because I am using that. I came upon this in my research: https://www.wpf-controls.com/wpf-smooth-scroll-viewer/ and was wondering about alternatives.
2 replies
CC#
Created by mario12136 on 7/13/2023 in #help
❔ When do you use "Model" suffix when naming models/classes?
I am using WPF and have a "Models" folder as well as one for view models and views. I was wondering when you add the "Model" suffix to your model names. Do you mix? Are they all without the suffix? Are they all with the suffix? I just wanted to hear what other developers and possibly the reasoning/explanation behind it.
3 replies
CC#
Created by mario12136 on 7/6/2023 in #help
❔ Settings model as a singleton in WPF
I have been using Properties.Settings.Default for my application which automatically saves a config file in the AppData\Local folder and I have been wanting to create a settings model to have more control over the file location and other things. Since my requirements were that there be a single instance that is accessible all throughout my app, I have found that this is called the Singleton pattern. The class I will create will read from and write as Json. My question is is this common and a correct use of the Singleton pattern? Is there something that I need to consider further before creating my Settings class. Also any relevant resources/guides would be appreciated. What I have
internal sealed class SettingsModel
{
private static SettingsModel _instance;

public static SettingsModel Instance
{
get
{
_instance ??= new SettingsModel();
return _instance;
}
}

private SettingsModel()
{
}

[JsonPropertyName("FontSize")]
public int FontSize { get; set; }

[JsonPropertyName("FontFamily")]
public FontFamily FontFamily { get; set; }

}
internal sealed class SettingsModel
{
private static SettingsModel _instance;

public static SettingsModel Instance
{
get
{
_instance ??= new SettingsModel();
return _instance;
}
}

private SettingsModel()
{
}

[JsonPropertyName("FontSize")]
public int FontSize { get; set; }

[JsonPropertyName("FontFamily")]
public FontFamily FontFamily { get; set; }

}
16 replies
CC#
Created by mario12136 on 7/6/2023 in #help
❔ How to use a readonly property for binding [AvalonEdit]?
Hello, I have created a behavior for AvalonEdit following the accepted answer here https://stackoverflow.com/questions/18964176/two-way-binding-to-avalonedit-document-text-using-mvvm. I want to have an instance of AvalonEdit that is readonly and I have a property in my viewmodel that looks liks this
public string SavedText => _document.Text;
public string SavedText => _document.Text;
I notify this property of change in one of my methods and it updates but the text of the readonly editor doesn't. The binding is OneWay and I have tried for example making a setter that does nothing for the property and the problem is solved for some reason. Note that whenever I call OnPropertyChanged(nameof(SavedText)) the property's value is updated but the callback function in the behavior is not called. I want a readonly property (doesn't make sense for it not to be) and a readonly editor and when the property is notified of changed the editor's text updates as well.
6 replies
CC#
Created by mario12136 on 6/29/2023 in #help
✅ Would this be wrong MVVM?
I made my own WPF "text editor" and I will just use the community toolkit's [RelayCommand] source generators to create commands to which I can bind to in my main view model. The reason they are not in the main view model is because execute and can execute for each command is highly dependent on my text editor's methods and properties, and I need to expose these commands somehow. Was wondering how people feel about this.
20 replies
CC#
Created by mario12136 on 6/28/2023 in #help
❔ How to create a bindable Text property for AvalonEdit?
I am using AvalonEdit in my application and I want to create a bindable text field. I understand why the authors did not make the original Text property bindable (for performance reasons) but for my use case I need it to work. I have checked this https://stackoverflow.com/questions/14855304/two-way-binding-in-avalonedit-doesnt-work and was wondering what is the cleanest simplest approach?
2 replies
CC#
Created by mario12136 on 6/27/2023 in #help
❔ Where are commands usually defined?
I ask to see what is regarded as best practice. I have commands in my view models, but I was wondering where commands specific to a text box (editor) are defined. Are they defined in the editor's class, a viewmodel for the editor or somewhere else? How does this generalize to any control for that matter? Think of commands specific to manipulating text only inside the editor.
30 replies
CC#
Created by mario12136 on 6/26/2023 in #help
❔ How to create commands for my subclassed AvalonEdit control?
Hello, I am working on a WPF application, a notepad clone, and I am using AvalonEdit as the main editor. Basically I have some MenuItems that I would like to bind them to commands, such as selecting the current line in the editor or deleting it. I also want to create shortcuts for these MenuItems. AvalonEdit already comes with some pre-defined commands according to the doc. I also am not sure how to do this in a correct MVVM way, since I am not sure where to put these specific text editing commands. Most commands I use just have a [RelayCommand] on top of a function I defined, and the reason I am confused is because I wouldn't put these editing commands in the main view model since it shouldn't know about the AvalonEdit. Scenario: Just to illustrate further, there is a duplicate line MenuItem in the app's main menubar. Pressing Ctrl+L also duplicates the current line (if I don't want to click on the MenuItem). Note: I have already defined functions that carry out functionality like duplicating the line and I currently have in code-behind ...Click += TextEditor.DuplicateLine... My questions: 1. How can I create these commands that I want? Where do I define them exactly? 2. How can I bind to AvalonEdit's predefined commands, like the delete line comand? 3. Do I have to create dependency properties for these commands in my subclassed AvalonEdit?
2 replies
CC#
Created by mario12136 on 3/19/2023 in #help
❔ Rider false positive: Cannot resolve symbol...
Hello, I am working in Rider on a WPF app and I keep getting Cannot resolve symbol for UI elements whose x:Name I have clearly defined. The code compiles without issue or warning but Rider is freaking out for some reason. The weird thing is I don't get this error for all controls, just the menu items for some reason. I rebuilt the solution and I even invalidated caches and restarted the IDE but I still get the same error. Any ideas for this?
12 replies
CC#
Created by mario12136 on 1/16/2023 in #help
Move Line Up in C# TextBox like in VS
I am working on a Notepad clone and I want to create the move line up/down feature that is so common in editors. I believe in VS it is Alt+Up/Alt+Down. I was wondering how can I do this, or if someone has done it before, if you don't mind sharing your code. I might probably have to remove the current line's content and then insert it at a higher line index but what if there is already a non-empty line above? Would appreciate help on this.
5 replies
CC#
Created by mario12136 on 1/15/2023 in #help
Handling MenuItem Clicks for my Notepad app: Maintaining MVVM and Best Practice
I am making what is essentially a WPF notepad clone with menu items that for example sort and trim lines and so on. I have created utility classes with functions that take and return strings and basically are the main functionality. I have subclassed the main text box and created functions for it that essentially apply the utility functions to the selected text and update it. In my MainView.xaml.cs, I handle the menu items click by connecting it to the text box's functions. This doesn't feel right and I am wondering what I can do. tl;dr I want to click a menu item in my notepad app that trims or sorts the currently selected text. What is the best way to achieve this. Source Code: https://gist.github.com/MarioFares/21349c3a4bdfb27eca1fd5abf7680b2d
3 replies