You are binding to a collection of objects. Can you see the data in the view? I ask as you are using binding with an ObservableCollection however your class is not implementing the
INotifyPropertyChanged[
^].
INotifyPropertyChanged
is required if you are setting values in your class object. Without it, the binding system will not see any changes.
You will need to do something like this:
1. Base class for
INotifyPropertyChanged
public abstract class ObservableBase : INotifyPropertyChanged
{
public void Set<TValue>(ref TValue field, TValue newValue, [CallerMemberName] string propertyName = "")
{
if (EqualityComparer<TValue>.Default.Equals(field, default(TValue)) || !field.Equals(newValue))
{
field = newValue;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
And the re-worked Class:
public class CL_Operation : ObservableBase
{
private string _strWidth;
public string strWidth
{
get => _strWidth;
set => Set(ref _strWidth, value);
}
private string _strAuart;
public string strAuart
{
get => _strAuart;
set => Set(ref _strAuart, value);
}
private string _strMaufnr;
public string strMaufnr
{
get => _strMaufnr;
set => Set(ref _strMaufnr, value);
}
private string _strNoOfOp;
public string strNoOfOp
{
get => _strNoOfOp;
set => Set(ref _strNoOfOp, value);
}
private string _strRueck;
public string strRueck
{
get => _strRueck;
set => Set(ref _strRueck, value);
}
private string _strCodeLigne;
public string strCodeLigne
{
get => _strCodeLigne;
set => Set(ref _strCodeLigne, value);
}
private string _strNumOp;
public string strNumOp
{
get => _strNumOp;
set => Set(ref _strNumOp, value);
}
private string _strDescriptionOp;
public string strDescriptionOp
{
get => _strDescriptionOp;
set => Set(ref _strDescriptionOp, value);
}
private string _strNoArticle;
public string strNoArticle
{
get => _strNoArticle;
set => Set(ref _strNoArticle, value);
}
private string _strDateFormatee;
public string strDateFormatee
{
get => _strDateFormatee;
set => Set(ref _strDateFormatee, value);
}
private string _strPcesPoste;
public string strPcesPoste
{
get => _strPcesPoste;
set => Set(ref _strPcesPoste, value);
}
private string _strTpsPoste;
public string strTpsPoste
{
get => _strTpsPoste;
set => Set(ref _strTpsPoste, value);
}
private string _strClient;
public string strClient
{
get => _strClient;
set => Set(ref _strClient, value);
}
private string _strTypeOp;
public string strTypeOp
{
get => _strTypeOp;
set => Set(ref _strTypeOp, value);
}
private string _strPriorite;
public string strPriorite
{
get => _strPriorite;
set => Set(ref _strPriorite, value);
}
private string _strPoste;
public string strPoste
{
get => _strPoste;
set => Set(ref _strPoste, value);
}
private string _strSsSect;
public string strSsSect
{
get => _strSsSect;
set => Set(ref _strSsSect, value);
}
private string _strTotaux;
public string strTotaux
{
get => _strTotaux;
set => Set(ref _strTotaux, value);
}
private DateTime _strDateDerBadge;
public DateTime strDateDerBadge
{
get => _strDateDerBadge;
set => Set(ref _strDateDerBadge, value);
}
private string _strTempsPoste;
public string strTempsPoste
{
get => _strTempsPoste;
set => Set(ref _strTempsPoste, value);
}
private string _strRetardAvanceText;
public string strRetardAvanceText
{
get => _strRetardAvanceText;
set => Set(ref _strRetardAvanceText, value);
}
private SolidColorBrush _scbColorNumOfOp;
public SolidColorBrush scbColorNumOfOp
{
get => _scbColorNumOfOp;
set => Set(ref _scbColorNumOfOp, value);
}
private SolidColorBrush _scbColorRetardAvance;
public SolidColorBrush scbColorRetardAvance
{
get => _scbColorRetardAvance;
set => Set(ref _scbColorRetardAvance, value);
}
private SolidColorBrush _scbBackColor;
public SolidColorBrush scbBackColor
{
get => _scbBackColor;
set => Set(ref _scbBackColor, value);
}
}
Update: Older C# Syntax:
public abstract class ObservableBase : INotifyPropertyChanged
{
public void Set<TValue>(ref TValue field, TValue newValue, [CallerMemberName] string propertyName = "")
{
if (EqualityComparer<TValue>.Default.Equals(field, default(TValue)) || !field.Equals(newValue))
{
field = newValue;
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class CL_Operation : ObservableBase
{
private string _strWidth;
public string strWidth
{
get { return _strWidth; }
set { Set(ref _strWidth, value); }
}
private string _strAuart;
public string strAuart
{
get
{
return _strAuart;
}
set
{
Set(ref _strAuart, value);
}
}
private string _strMaufnr;
public string strMaufnr
{
get
{
return _strMaufnr;
}
set
{
Set(ref _strMaufnr, value);
}
}
private string _strNoOfOp;
public string strNoOfOp
{
get
{
return _strNoOfOp;
}
set
{
Set(ref _strNoOfOp, value);
}
}
private string _strRueck;
public string strRueck
{
get
{
return _strRueck;
}
set
{
Set(ref _strRueck, value);
}
}
private string _strCodeLigne;
public string strCodeLigne
{
get
{
return _strCodeLigne;
}
set
{
Set(ref _strCodeLigne, value);
}
}
private string _strNumOp;
public string strNumOp
{
get
{
return _strNumOp;
}
set
{
Set(ref _strNumOp, value);
}
}
private string _strDescriptionOp;
public string strDescriptionOp
{
get
{
return _strDescriptionOp;
}
set
{
Set(ref _strDescriptionOp, value);
}
}
private string _strNoArticle;
public string strNoArticle
{
get
{
return _strNoArticle;
}
set
{
Set(ref _strNoArticle, value);
}
}
private string _strDateFormatee;
public string strDateFormatee
{
get
{
return _strDateFormatee;
}
set
{
Set(ref _strDateFormatee, value);
}
}
private string _strPcesPoste;
public string strPcesPoste
{
get
{
return _strPcesPoste;
}
set
{
Set(ref _strPcesPoste, value);
}
}
private string _strTpsPoste;
public string strTpsPoste
{
get
{
return _strTpsPoste;
}
set
{
Set(ref _strTpsPoste, value);
}
}
private string _strClient;
public string strClient
{
get
{
return _strClient;
}
set
{
Set(ref _strClient, value);
}
}
private string _strTypeOp;
public string strTypeOp
{
get
{
return _strTypeOp;
}
set
{
Set(ref _strTypeOp, value);
}
}
private string _strPriorite;
public string strPriorite
{
get
{
return _strPriorite;
}
set
{
Set(ref _strPriorite, value);
}
}
private string _strPoste;
public string strPoste
{
get
{
return _strPoste;
}
set
{
Set(ref _strPoste, value);
}
}
private string _strSsSect;
public string strSsSect
{
get
{
return _strSsSect;
}
set
{
Set(ref _strSsSect, value);
}
}
private string _strTotaux;
public string strTotaux
{
get
{
return _strTotaux;
}
set
{
Set(ref _strTotaux, value);
}
}
private DateTime _strDateDerBadge;
public DateTime strDateDerBadge
{
get
{
return _strDateDerBadge;
}
set
{
Set(ref _strDateDerBadge, value);
}
}
private string _strTempsPoste;
public string strTempsPoste
{
get
{
return _strTempsPoste;
}
set
{
Set(ref _strTempsPoste, value);
}
}
private string _strRetardAvanceText;
public string strRetardAvanceText
{
get
{
return _strRetardAvanceText;
}
set
{
Set(ref _strRetardAvanceText, value);
}
}
private SolidColorBrush _scbColorNumOfOp;
public SolidColorBrush scbColorNumOfOp
{
get
{
return _scbColorNumOfOp;
}
set
{
Set(ref _scbColorNumOfOp, value);
}
}
private SolidColorBrush _scbColorRetardAvance;
public SolidColorBrush scbColorRetardAvance
{
get
{
return _scbColorRetardAvance;
}
set
{
Set(ref _scbColorRetardAvance, value);
}
}
private SolidColorBrush _scbBackColor;
public SolidColorBrush scbBackColor
{
get
{
return _scbBackColor;
}
set
{
Set(ref _scbBackColor, value);
}
}
}
UPDATE #2: Here is an example of simple binding using the above
ObservableBase
class. Many others are on the internet...
public class PersonModel : ObservableBase
{
private string name;
public string Name
{
get { return name; }
set { Set(ref name, value); }
}
private int age;
public int Age
{
get { return age; }
set { Set(ref age, value); }
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
Mock();
}
public ObservableCollection<PersonModel> Persons { get; set; }
= new ObservableCollection<PersonModel>();
private Random rand = new Random();
private void Mock()
{
for (int i = 0; i < 10; i++)
{
Persons.Add(new PersonModel
{
Name = string.Format("Person {0}", i),
Age = rand.Next(20, 50)
});
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (var person in Persons)
{
person.Age = rand.Next(20, 50);
}
}
}
And the page:
<Window
x:Class="WpfSimpleBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfSimpleBinding"
Title="Basic code-behin binding example"
WindowStartupLocation="CenterScreen" Height="400" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.Resources>
<DataTemplate DataType="{x:Type local:PersonModel}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Margin="10 3"/>
<TextBlock Text="{Binding Age}" Margin="10 3"
Grid.Column="1"/>
</Grid>
</DataTemplate>
</Grid.Resources>
<ListBox ItemsSource="{Binding Persons}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button Content="Randomize" Padding="10 5" Margin="10"
HorizontalAlignment="Center" VerticalAlignment="Center"
Grid.Row="1" Click="Button_Click"/>
</Grid>
</Window>
Click the button and watch the information (age) update using the binding system.