I am trying to learn databinding and new in C#. To isolate the code from UI I want to bind the value from the XAML. And I made the following example to ask my question as clear as possible:
Here is the program. I want the progress bar to increase or decrease by using increase or decrease buttons but these should be tied to "value" parameter in a class. The increaseValue method in the class Power returns value and I want to bind that value to the button and the progress bar. So when the user clicks on the button the value will increase and also the progress bar will be tied to the new value:
XAML code:
<Window x:Class="WpfBindingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfBindingTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Button x:Name="Increase" Content="Increase" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Button>
<Button Grid.Row="1" x:Name="Decrease" Content="Decrease" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Button>
<ProgressBar Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="10"></ProgressBar>
<Label Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Center"></Label>
</Grid>
</Window>
MainWindow.xaml.cs:
namespace WpfBindingTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Power power = new Power();
public MainWindow()
{
InitializeComponent();
}
}
}
class Power:
namespace WpfBindingTest
{
public class Power
{
public int increaseValue(int value)
{
value++;
return value;
}
}
}
I can achieve what I want by code but I want to isolate the Power class from UI but super confused about how to do data binding.