Reputation: 61
Hokkay...
So I'm certain I'm just doing wrong something fairly rudimentary but, for whatever, when I change the value in my class, the datacontext stores the right value but the value is not sent to the textblock.
namespace Points_Calculator
{
public partial class MainWindow : MetroWindow
{
private PointsModel Model = new PointsModel();
public MainWindow()
{
InitializeComponent();
DataContext = Model;
}
private void Process()
{
if(Model.ValidInput())
{
double K = Model.Kcal / Denum.Kcal;
double F = Model.Fat / Denum.Fat;
double A = Math.Round(K + F);
//outPoints.Text = A.ToString() + " Points";
Model.Points = A.ToString() + " Points";
}
}
private void inputFat_TextChanged(object sender, TextChangedEventArgs e)
{
Process();
}
private void inputKcal_TextChanged(object sender, TextChangedEventArgs e)
{
Process();
}
}
}
The problem is in Process(). See where I commented out the bit about changing the Text property of outPoints (TextBlock)? That works fine. But I want to use the databinding, which is the next line. The correct value is set in Model.Points, as it should, but for some reason, it is not reflected in the TextBlock.
<TextBlock Name="outPoints" Text="{Binding Points, Mode=TwoWay}"
Margin="0,20,-25,0"
FontSize="16"
Grid.Row="2"
TextAlignment="Center"
Grid.ColumnSpan="2" />
Can anyone point out what I'm doing wrong? I would really appreciated it.
Edit: Many thanks to Decoherence for the advice and for those who have the same problem, I'm linking the relevant How to: Implement the INotifyPropertyChanged Interface page.
Upvotes: 0
Views: 86
Reputation: 2372
Your PointsModel
has to implement the INotifyPropertyChanged interface and the setter of the Points
property should call NotifyPropertyChanged
, so the problem is actually with your PointsModel
.
Upvotes: 1