Skip to content

Commit fd5d3fd

Browse files
committed
Data binding notes updated
1 parent 9606c9f commit fd5d3fd

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

docs/WPF/3 DataBind.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,56 @@
1-
### Text Property
1+
### Databinding
2+
- For Databinding you have to implement INotifyPropertyChanged interface
3+
- Which contain PropertyChangedEventHandler, which we can trigger everytime we update our property
4+
5+
### Sample Code
26
```xml
3-
Text="{Binding ApplicationName,RelativeSource={RelativeSourceAncestorType=Window}}"
7+
Title="Test" Height="250" Width="300">
8+
<Grid>
9+
<Grid.RowDefinitions>
10+
<RowDefinition/>
11+
<RowDefinition/>
12+
<RowDefinition/>
13+
<RowDefinition/>
14+
</Grid.RowDefinitions>
15+
<TextBox Grid.Row="0" Name="NameTextBox" Height="40" Width="250" Text="{Binding ClientName,UpdateSourceTrigger=PropertyChanged}"/>
16+
<Button Grid.Row="1" Name="SubmitButton" Height="40" Width="100" Content="Submit" Click="SubmitButton_Click"/>
17+
<TextBlock Grid.Row="2" Name="ResultTextBlock" Height="40" Width="250" Text="{Binding ClientName}"/>
18+
<Button Grid.Row="3" Name="UpdateButton" Height="40" Width="100" Content="Update Name" Click="UpdateNameButton_Click"/>
19+
</Grid>
20+
```
21+
```csharp
22+
public partial class Test : Window, INotifyPropertyChanged
23+
{
24+
public Test()
25+
{
26+
DataContext = this;
27+
InitializeComponent();
28+
}
29+
30+
private string _name = string.Empty;
31+
public string ClientName
32+
{
33+
get { return _name; }
34+
set
35+
{
36+
_name = value;
37+
OnPropertyChanged(nameof(ClientName));
38+
}
39+
}
40+
41+
public event PropertyChangedEventHandler PropertyChanged;
42+
43+
protected virtual void OnPropertyChanged(string propertyName)
44+
{
45+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
46+
}
47+
private void SubmitButton_Click(object sender, RoutedEventArgs e)
48+
{
49+
MessageBox.Show($"Hello, {ClientName}!");
50+
}
51+
private void UpdateNameButton_Click(object sender, RoutedEventArgs e)
52+
{
53+
ClientName = "Nodes Automations";
54+
}
55+
}
456
```

0 commit comments

Comments
 (0)