C
C#13mo ago
Core Dream

❔ WF to WPF Help Needed

Hello sharpers. I have a issue where I need to convert at least the LoadFileSyncs to work with CommunityToolkit.MVVM in WPF I know how to Bind a ICommand to a button, I know how to do very basic ViewModel stuff so far but this is very new to me. OG Code is @ https://pastebin.com/EQyMRsvf My VM is @ https://pastebin.com/7cxT0ntv I appreciate you all!
Pastebin
using System.Data;using TitanSTUDIO.Helpers.Parsers;using TitanSTUD...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
using CommunityToolkit.Mvvm.ComponentModel;using CommunityToolkit.M...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
3 Replies
Core Dream
Core DreamOP13mo ago
😦
Klarth
Klarth13mo ago
Is this an arbitrary schema? Or is it exactly known at compile time? For a fixed schema, I would start off with something like:
public partial class MainAppViewModel : ObservableObject
{
[ObservableProperty] private StringDatabaseModel? _sdbModel;
[ObservableProperty] private string? _id;
[ObservableProperty] private string? _text;
[ObservableProperty] private string? _offset;

[RelayCommand]
public async Task LoadFileAsync(string filename)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "String Database File (*.sdb)|*.sdb";
ofd.RestoreDirectory = true;

if (ofd.ShowDialog() == DialogResult.OK)
{
// Clear the search textbox
SearchTermText.Clear(); // Probably create a new property and expose a binding here instead

currentLoadedFile = ofd.FileName;
SdbModel = await StringDatabase.ReadStringAsync(currentLoadedFile);

UpdateDataGrid(); // Use bindings instead

// Update the status information for the user to see.
UpdateStatus(); // Use bindings or messaging instead
}
}
}
}
public partial class MainAppViewModel : ObservableObject
{
[ObservableProperty] private StringDatabaseModel? _sdbModel;
[ObservableProperty] private string? _id;
[ObservableProperty] private string? _text;
[ObservableProperty] private string? _offset;

[RelayCommand]
public async Task LoadFileAsync(string filename)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "String Database File (*.sdb)|*.sdb";
ofd.RestoreDirectory = true;

if (ofd.ShowDialog() == DialogResult.OK)
{
// Clear the search textbox
SearchTermText.Clear(); // Probably create a new property and expose a binding here instead

currentLoadedFile = ofd.FileName;
SdbModel = await StringDatabase.ReadStringAsync(currentLoadedFile);

UpdateDataGrid(); // Use bindings instead

// Update the status information for the user to see.
UpdateStatus(); // Use bindings or messaging instead
}
}
}
}
<Button Command="{Binding LoadFileAsyncCommand}" /> The DB stuff is more complex. If it's arbitrary schema, then you need to use DataTable as part of your DataGrid. If it's a static schema, then as long as your StringDatabaseModel is well-designed, you can bind rows/columns easily in XAML.
Accord
Accord13mo 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.
Want results from more Discord servers?
Add your server