Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to do binding in Wpf C#

I tring to do binding

when select item form combobox( Item Top,Bottom,Left,Right)


I have 6 text box to update value (min and max display) but its not updating.

Need some one help..

Thanks in advance..

What I have tried:

public partial class TeachMenu : Page
{

private ObservableCollection<parampos> Param;
public TeachMenu()
{
InitializeComponent();


Param1 = new ObservableCollection<posparam>()
{
new PosParam() { R_min = 1, R_max = 255, G_min = 1, G_max = 255, B_min = 1, B_max = 255 },
new PosParam() { R_min = 2, R_max = 255, G_min = 2, G_max = 255, B_min = 2, B_max = 255 },
new PosParam() { R_min = 3, R_max = 255, G_min = 3, G_max = 255, B_min = 3, B_max = 255 },
new PosParam() { R_min = 4, R_max = 255, G_min = 4, G_max = 255, B_min = 4, B_max = 255 }
};
TabPositioning.DataContext = Param;

}

public class Param
{

public int R_min { get => r_min; set => r_min = value; }
public int R_max { get => r_max; set => r_max = value; }
public int G_min { get => g_min; set => g_min = value; }
public int G_max { get => g_max; set => g_max = value; }
public int B_min { get => b_min; set => b_min = value; }
public int B_max { get => b_max; set => b_max = value; }
public double Row1 { get => row1; set => row1 = value; }
public double Col1 { get => col1; set => col1 = value; }
public double Row2 { get => row2; set => row2 = value; }
public double Col2 { get => col2; set => col2 = value; }

public enum PosFrame
{
Frame,
Top,
Bottom,
Left,
Right
}
private int r_min;
private int r_max;
private int g_min;
private int g_max;
private int b_min;
private int b_max;

private double row1;
private double col1;
private double row2;
private double col2;

}
Posted
Updated 17-Jul-18 21:09pm
Comments
Ajcek84 17-Jul-18 5:51am    
You should show us what your XAML view looks like.
It is also a good idea to move model to another class like TeachMenuViewModel and then set page's DataContext to it's instance.
Member 13157670 17-Jul-18 20:54pm    
Xaml:

<ComboBox x:Name="cbPosFrame" HorizontalAlignment="left" Margin="56,120,0,772" Width="89" RenderTransformOrigin="0.993,0.521"/>

<Slider HorizontalAlignment="Left" Margin="170,210,0,0" VerticalAlignment="Top" Height="30" Width="414" Minimum="0" Maximum="255" Value="{Binding Path=R_min,Mode=TwoWay}">
<TextBox HorizontalAlignment="Left" Height="28" Margin="80,210,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="65" Text="{Binding Path=R_min, Mode=TwoWay}">


<Slider HorizontalAlignment="Left" Height="28" Margin="170,252,0,0" VerticalAlignment="Top" Width="414" Minimum="0" Maximum="255" Value="{Binding Path=R_max,Mode=TwoWay}">
<TextBox HorizontalAlignment="Left" Height="28" Margin="80,252,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="65" Text="{Binding Path=R_max, Mode=TwoWay}">


<Slider HorizontalAlignment="Left" Height="28" Margin="170,292,0,0" VerticalAlignment="Top" Width="414" Minimum="0" Maximum="255" Value="{Binding Path=G_min,Mode=TwoWay}">
<TextBox HorizontalAlignment="Left" Height="28" Margin="80,292,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="65" Text="{Binding Path=G_min, Mode=TwoWay}">


<Slider HorizontalAlignment="Left" Height="28" Margin="170,330,0,0" VerticalAlignment="Top" Width="414" Minimum="0" Maximum="255" Value="{Binding Path=G_max,Mode=TwoWay}">
<TextBox HorizontalAlignment="Left" Height="28" Margin="80,330,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="65" Text="{Binding Path=G_max, Mode=TwoWay}">


<Slider HorizontalAlignment="Left" Height="28" Margin="170,368,0,0" VerticalAlignment="Top" Width="414" Minimum="0" Maximum="255" Value="{Binding Path=B_min,Mode=TwoWay}">
<TextBox HorizontalAlignment="Left" Height="28" Margin="80,368,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="65" Text="{Binding Path=B_min, Mode=TwoWay}">


<Slider HorizontalAlignment="Left" Height="28" Margin="170,404,0,0" VerticalAlignment="Top" Width="414" Minimum="0" Maximum="255" Value="{Binding Path=B_max,Mode=TwoWay}">
<TextBox HorizontalAlignment="Left" Height="28" Margin="80,404,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="65" Text="{Binding Path=B_max, Mode=TwoWay}">
Member 13157670 17-Jul-18 20:56pm    
This is Xaml program... When User change Slider position, The text box also change through binding object..

Binding works with Properties, not Fields. So change:
C#
private ObservableCollection<parampos> Param;

with
C#
public ObservableCollection<parampos> Param {get; set;}

Also, to clear an ObservableCollection, always use the Clear() method and not
C#
new ObservableCollection<...>();
as the latter with break the binding.

You can read more about this here: Data Binding (WPF) | Microsoft Docs[^]
 
Share this answer
 
Hi ,

I have attached code.. When select combo Box item... Slider and Textbox value should change..Thank you for help..

Binding - Google Drive[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900