C
C#3mo ago
intee_

✅ 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; }
}
}
25 Replies
aodpi
aodpi3mo ago
In this case nothing will display in data bindings because your ViewModel doesn’t implement the ‘INotifyPropertyChanged’ interface
sibber
sibber3mo ago
wdym? the button doesnt do anything
intee_
intee_OP3mo ago
But it is working :\
sibber
sibber3mo ago
also
- private string displayText;
- public string DisplayText {
- get { return displayText; }
- set { displayText = value; }
- }
+ public string DisplayText { get; set; }
- private string displayText;
- public string DisplayText {
- get { return displayText; }
- set { displayText = value; }
- }
+ public string DisplayText { get; set; }
intee_
intee_OP3mo ago
I am using a base for my view mode that is using INotify but it's not being inherited anywhere
sibber
sibber3mo ago
wdym by working?
intee_
intee_OP3mo ago
If I type into the text box and press the button, the text is displayed in the textblock
No description
intee_
intee_OP3mo ago
And I just checked, I haven't even changed my ViewModel bases namespace from another project I copied it from.
sibber
sibber3mo ago
huh so are you raising a change notification anywhere?
intee_
intee_OP3mo ago
I'm going to say no but not 100% sure. The only place I have an event is in the base
sibber
sibber3mo ago
can you show you entire viewmodel?
intee_
intee_OP3mo ago
This is the base if it matters, but yeah NAmespace is wrong and I'm not inheriting it as far as I know.
namespace Basic_Tutorial_Rerun.MVVM {

internal class ViewModel_Base : INotifyPropertyChanged {

public event PropertyChangedEventHandler? PropertyChanged;

protected void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

}
}
namespace Basic_Tutorial_Rerun.MVVM {

internal class ViewModel_Base : INotifyPropertyChanged {

public event PropertyChangedEventHandler? PropertyChanged;

protected void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

}
}
namespace NoGoogle_MVVM_Test.ViewModel {
class MainWindow_ViewModel
{
private string displayText;

public string DisplayText {
get { return displayText; }
set { displayText = value; }
}

}
}
namespace NoGoogle_MVVM_Test.ViewModel {
class MainWindow_ViewModel
{
private string displayText;

public string DisplayText {
get { return displayText; }
set { displayText = value; }
}

}
}
sibber
sibber3mo ago
your viewmodel doesnt inherit from your base? also the underscores in names are very unconventional
intee_
intee_OP3mo ago
Yeah, it's muscel memory still haha.
sibber
sibber3mo ago
oh yeah wtf any random button makes the binding update somehow
intee_
intee_OP3mo ago
I tried putting it into a different grid as well (Within the main one, whatever it's called) and same thing. That is about all I tried if I'm being honest though haha.
sibber
sibber3mo ago
ohh i see whats happening here its weird wpf defaults the default binding mode for text boxes in wpf is two way, and the default UpdateSourceTrigger is LostFocus so when you type in the textbox, it sets the property, and when you press the button, the focus trigger is triggered and it reads the new property
intee_
intee_OP3mo ago
Ohh, so whenever I click set, I'm jsut losing focus. Ahhh, thanks so much mate!
sibber
sibber3mo ago
yeah, whenever the textbox loses focus
intee_
intee_OP3mo ago
I would never have figured that out haha.
sibber
sibber3mo ago
yeah thats why i like to explicitly set these things, not leave the defaults
intee_
intee_OP3mo ago
Yeah, I'll keep defaults in mind from now on for sure haha. Thanks again!
sibber
sibber3mo ago
no worries also keep in mind that the defaults may vary between controls
The default value varies for each dependency property. In general, user-editable control properties, such as those of text boxes and check boxes, default to two-way bindings, whereas most other properties default to one-way bindings.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.data.bindingmode?view=windowsdesktop-8.0#system-windows-data-bindingmode-default $close @intee_
MODiX
MODiX3mo ago
If you have no further questions, please use /close to mark the forum thread as answered
intee_
intee_OP3mo ago
Legend man. thanks for all the help! It's making so much more sense now hahah..
Want results from more Discord servers?
Add your server