intee_
intee_
CC#
Created by intee_ on 10/1/2024 in #help
✅ Deserialize JSON back into custom object
Hey all, I have an ObservableCollection that I am using to store a list of tasks (A custom object "TaskItem" that is build like this.
c#

internal class TaskItem {

public string Task { get; set; }
public string Description { get; set; }
public string Date { get; }

public TaskItem()
{
Date = DateTime.Now.ToString("hh:mmtt dd/MMM/yyyy");
}
}
c#

internal class TaskItem {

public string Task { get; set; }
public string Description { get; set; }
public string Date { get; }

public TaskItem()
{
Date = DateTime.Now.ToString("hh:mmtt dd/MMM/yyyy");
}
}
And in my ViewModel I'm creating an ObservableCollection of those item types, called "TaskList". I am exporting that list of TaskItems to Json and that works fine with the below:
c#
string json = JsonConvert.SerializeObject(TaskList);
File.WriteAllText(SavePath, json)
c#
string json = JsonConvert.SerializeObject(TaskList);
File.WriteAllText(SavePath, json)
But I can't seem to work out how the hell I load that back into TaskList variable when I re-open the app. I have been messing around with this in the ViewModel constructor:
c#
string loadedJSON = File.ReadAllText(SavePath);
TaskList = JsonConvert.DeserializeObject<TaskItem>(loadedJSON);
c#
string loadedJSON = File.ReadAllText(SavePath);
TaskList = JsonConvert.DeserializeObject<TaskItem>(loadedJSON);
Trieda bunch of different stuff in there, this was just what I gave up on haha. Am I at least getting close? haha.
6 replies
CC#
Created by intee_ on 9/30/2024 in #help
✅ Change WPF element style depending on property value
No description
1 replies
CC#
Created by intee_ on 9/27/2024 in #help
✅ How does the button know what to do?
Hey, new to C# and wrapping my head around following MVVM. Can someone help me understand how the button knows what to do here? When adding a new button or data binding, they all trigger each other (if that makes sense...). Will any button trigger all data bindings unless otherwise set with like a command relay or something? XAML:
<TextBlock Text="{Binding DisplayText}" Background="LightGray" Margin="10"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
<TextBox Width="550" Height="30" Margin="10, 0,0,0" Text="{Binding DisplayText}"/>
<Button Content="Set" Width="200" Height="30" />
</StackPanel>
<TextBlock Text="{Binding DisplayText}" Background="LightGray" Margin="10"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
<TextBox Width="550" Height="30" Margin="10, 0,0,0" Text="{Binding DisplayText}"/>
<Button Content="Set" Width="200" Height="30" />
</StackPanel>
Code Behind:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
MainWindow_ViewModel vm = new MainWindow_ViewModel();
DataContext = vm;
}
}
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
MainWindow_ViewModel vm = new MainWindow_ViewModel();
DataContext = vm;
}
}
View Model:
class MainWindow_ViewModel
{
private string displayText;
public string DisplayText {
get { return displayText; }
set { displayText = value; }
}
}
class MainWindow_ViewModel
{
private string displayText;
public string DisplayText {
get { return displayText; }
set { displayText = value; }
}
}
39 replies
CC#
Created by intee_ on 9/27/2024 in #help
✅ Invoking an event on property changed (WPF)
Hey all, Just wondering if someone can help me wrap my head around how this works, I am not sure where I am getting caught up but I am sure I am not understanding something. Just started learning WPF and C#. Sorry for the data dump. I have a text box, a button and a textblock. I am just trying to make the text from the text box be set in the textblock. I have have my bindings working but I don't understand HOW they are working. I have a PropertyChangedEventHandler in my main window code behind and I have the public and private properties for the binding as well. In the setter of the binding I am calling PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DisplayText")); This is where I am getting confused I think. When a property is changed on the UI the properties setter is called by that event handler (Handled by INotifyPropertyChanged) (???) How exactly does this part of it work? I'm not using any relay commands or anything like that, just straight data binding from the UI to the code behind. Hope this makes at least a little sense...
11 replies