Post: Databinding - Get rid of unnecessary code! [C#]
09-16-2015, 10:19 PM #1
jagex
Gym leader
(adsbygoogle = window.adsbygoogle || []).push({}); Hey, quick tutorial on Databinding! Not the greatest at it but I will share what I know with examples.

This tutorial is for WPF. Databinding works for WinForms as well but I think its done differently

Example 1:
Single Binding
If you want a button to enable itself depending whether checkbox(es) are checked or not you would normally do something along the lines of this
    
if(checkBox1.IsChecked.Value)
{
btnSumbit.IsEnabled = true;
}
else
{
btnSumbit.IsEnabled = false;
}


With databinding, we can get rid of all that code and do it in one line in xaml! This is done by adding a binding to the isEnabled property of the button

    
IsEnabled="{Binding ElementName=checkBox1, Path=IsChecked}"



    
<Button x:Name="btnSumbit" IsEnabled="{Binding ElementName=checkBox1, Path=IsChecked}" Content="Button" HorizontalAlignment="Left" Margin="49,121,0,0" VerticalAlignment="Top" Width="75"/>


Simple as that. The button will now enable/disable itself depending whether or not checkBox1 is checked or not

Example 2:
MultiBinding
But what if I want to enable a button depending on whether multiple checkboxes are checked??
No worries, you can use MultiBinding with MultiConverter Interface!

Create a class
Implement the IMultiConverter interface

Your class should look like this
You must login or register to view this content.

Go to your xaml code and add your converter class as a resource like this
    <Window.Resources>
<local:IsEnabled x:Key="IsEnabledConverter"/>
</Window.Resources>


This is how you bind multiple checkboxes to a buttons IsEnabled property
You must login or register to view this content.

Now go to your converter class and add this code in the Convert method
    
if((bool) values[0] && (bool) values[1])
{
return true;
}
return false;
}

Thats it! Now whenever checkbox1 or checkbox2 is checked/unchecked, the converter class Convert Method will run. If both checkboxes are checked, the button will enable. If both checkboxes are not checked, the button will disable itself.

There are a lot of ways you can use Databinding, like binding control values to variables and vice versa. Ex(binding textbox.text values to a variable). So with that, the need of doing textbox.text = myvariable is not required.
Last edited by jagex ; 09-16-2015 at 10:44 PM.

The following 2 users say thank you to jagex for this useful post:

Trefad
09-16-2015, 10:40 PM #2
Trefad
I defeated!
Thanks! I found this helpful Smile

The following user thanked Trefad for this useful post:

jagex
09-16-2015, 10:44 PM #3
jagex
Gym leader
Originally posted by Trefad View Post
Thanks! I found this helpful Smile


Glad you found it helpful!
09-18-2015, 01:32 PM #4
Default Avatar
Oneup
Guest
Originally posted by jagex View Post
Hey, quick tutorial on Databinding! Not the greatest at it but I will share what I know with examples.

This tutorial is for WPF. Databinding works for WinForms as well but I think its done differently

Example 1:
Single Binding
If you want a button to enable itself depending whether checkbox(es) are checked or not you would normally do something along the lines of this
    
if(checkBox1.IsChecked.Value)
{
btnSumbit.IsEnabled = true;
}
else
{
btnSumbit.IsEnabled = false;
}


With databinding, we can get rid of all that code and do it in one line in xaml! This is done by adding a binding to the isEnabled property of the button

    
IsEnabled="{Binding ElementName=checkBox1, Path=IsChecked}"



    
<Button x:Name="btnSumbit" IsEnabled="{Binding ElementName=checkBox1, Path=IsChecked}" Content="Button" HorizontalAlignment="Left" Margin="49,121,0,0" VerticalAlignment="Top" Width="75"/>


Simple as that. The button will now enable/disable itself depending whether or not checkBox1 is checked or not

Example 2:
MultiBinding
But what if I want to enable a button depending on whether multiple checkboxes are checked??
No worries, you can use MultiBinding with MultiConverter Interface!

Create a class
Implement the IMultiConverter interface

Your class should look like this
You must login or register to view this content.

Go to your xaml code and add your converter class as a resource like this
    <Window.Resources>
<local:IsEnabled x:Key="IsEnabledConverter"/>
</Window.Resources>


This is how you bind multiple checkboxes to a buttons IsEnabled property
You must login or register to view this content.

Now go to your converter class and add this code in the Convert method
    
if((bool) values[0] && (bool) values[1])
{
return true;
}
return false;
}

Thats it! Now whenever checkbox1 or checkbox2 is checked/unchecked, the converter class Convert Method will run. If both checkboxes are checked, the button will enable. If both checkboxes are not checked, the button will disable itself.

There are a lot of ways you can use Databinding, like binding control values to variables and vice versa. Ex(binding textbox.text values to a variable). So with that, the need of doing textbox.text = myvariable is not required.


When I read the title I thought you were talking about from a data source.
Either way this is actually pretty cool. I haven't played with WPF much.

The following user thanked Oneup for this useful post:

jagex
09-25-2015, 02:12 PM #5
Originally posted by jagex View Post
Hey, quick tutorial on Databinding! Not the greatest at it but I will share what I know with examples.

This tutorial is for WPF. Databinding works for WinForms as well but I think its done differently

Example 1:
Single Binding
If you want a button to enable itself depending whether checkbox(es) are checked or not you would normally do something along the lines of this
    
if(checkBox1.IsChecked.Value)
{
btnSumbit.IsEnabled = true;
}
else
{
btnSumbit.IsEnabled = false;
}


With databinding, we can get rid of all that code and do it in one line in xaml! This is done by adding a binding to the isEnabled property of the button

    
IsEnabled="{Binding ElementName=checkBox1, Path=IsChecked}"



    
<Button x:Name="btnSumbit" IsEnabled="{Binding ElementName=checkBox1, Path=IsChecked}" Content="Button" HorizontalAlignment="Left" Margin="49,121,0,0" VerticalAlignment="Top" Width="75"/>


Simple as that. The button will now enable/disable itself depending whether or not checkBox1 is checked or not

Example 2:
MultiBinding
But what if I want to enable a button depending on whether multiple checkboxes are checked??
No worries, you can use MultiBinding with MultiConverter Interface!

Create a class
Implement the IMultiConverter interface

Your class should look like this
You must login or register to view this content.

Go to your xaml code and add your converter class as a resource like this
    <Window.Resources>
<local:IsEnabled x:Key="IsEnabledConverter"/>
</Window.Resources>


This is how you bind multiple checkboxes to a buttons IsEnabled property
You must login or register to view this content.

Now go to your converter class and add this code in the Convert method
    
if((bool) values[0] && (bool) values[1])
{
return true;
}
return false;
}

Thats it! Now whenever checkbox1 or checkbox2 is checked/unchecked, the converter class Convert Method will run. If both checkboxes are checked, the button will enable. If both checkboxes are not checked, the button will disable itself.

There are a lot of ways you can use Databinding, like binding control values to variables and vice versa. Ex(binding textbox.text values to a variable). So with that, the need of doing textbox.text = myvariable is not required.


If I understand what you're doing correctly, there's a much shorter way of going about this
    
btnSumbit.IsEnabled = checkBox1.IsChecked.Value;
09-25-2015, 05:05 PM #6
jagex
Gym leader
Did you read all of it? What in doing is completely different Gasp
09-27-2015, 03:18 AM #7
Do you know if there is a major performance difference between the two examples you gave?
09-27-2015, 05:27 AM #8
jagex
Gym leader
Originally posted by Hybrii View Post
Do you know if there is a major performance difference between the two examples you gave?


You must login or register to view this content.

Check that out, should answer any questions you have about it.

Advantages of DataBinding

Databinding in .NET can be used to write data driven applications quickly. .NET data binding allows you to write less code with fast execution but still get the work done in the best way.

.NET automatically writes a lot of databinding code for you in the background (you can see it in "Windows Generated Code" section), so the developer does not have to spend time writing code for basic databinding, but still has the flexibility of modifying any code that he would like to. We get the benefits of bound as well as unbound approach.

Control over the Databinding process by using events. This is discussed in more detail later in the article.

Disadvantages of DataBinding

More optimized code can be written by using the unbound or traditional methods.

Complete flexibility can only be achieved by using the unbound approach.
Last edited by jagex ; 09-27-2015 at 05:31 AM.
09-28-2015, 11:24 AM #9
Default Avatar
Oneup
Guest
Originally posted by Hybrii View Post
Do you know if there is a major performance difference between the two examples you gave?


Unless you are writing an enterprise level program it's not really going to make much of a difference in terms of scaling.
Would it be a better practice? Yea I wouldn't hurt.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo