Child Actions Are Not Allowed to Perform Redirect Actions, After Setting the Site on Https

WPF DataBinding not updating?

In order to support data binding, your data object must implement INotifyPropertyChanged

Also, it's always a good idea to Separate Data from Presentation

public class ViewModel: INotifyPropertyChanged
{
private bool _test;
public bool Test
{ get { return _test; }
set
{
_test = value;
NotifyPropertyChanged("Test");
}
}

public PropertyChangedEventHandler PropertyChanged;

public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

<Window x:Class="TheTestingProject_WPF_.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Viewbox>
<CheckBox IsChecked="{Binding Path=Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Viewbox>
</Grid>

Code Behind:

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel{Test = true};
}
}

XAML Data Binding not updating UI when property changes

Your issue is with the way you are binding the DataContext to the individual controls. Your ViewModel is not automatically a Singleton (single instance only object) so each time you specify it, you're actually creating a separate instance.

If you set the DataContext at the Window level, your code should work as expected.

WPF binding not updating the view

You need to implement INotifyPropertyChanged in your ViewModel order to notify the View that the property has changed.

Here's a link to the MSDN page for it: System.ComponentModel.INotifyPropertyChanged

The most important thing to note is that you should raise the PropertyChanged event in your property setter.

WPF Binding not updating XAML but PropertyChanged called

The reason why the binding to DisplayPopup does not update is that the property is a computed property. A computed property lacks a setter and therefore never raises INotifyPropertyChanged.PropertyChanged. The data binding listens to this event. As the result DisplayPopup.Get is only called once (the moment the binding is initialized).

To solve this you can either let the MainViewModel listen to PropertyChanged events of the ContactViewModel items or as it seems that you are interested in selected items simply bind the ListBox.SelectedItem and change MainViewModel.DisplayPopup on changes.

For simplicity I recommend the second solution.

Note that in order to make the ListBox.IsSelected binding work you must set the ListBox.ItemContainerStyle and target ListBoxItem instead of ListBox:

MainViewModel.cs

public class MainViewModel : BaseViewModel
{
public ObservableCollection<ContactViewModel> TankItems { get; set; }

private ContactViewModel selectedTankItem;
public ContactViewModel SelectedTankItem
{
get => this.selectedTankItem;
set
{
this.selectedTankItem = value;
OnPropertyChanged(nameof(this.SelectedTankItem));

this.DisplayPopup = this.SelectedTankItem != null;
}

// Raises INotifyPropertyChanged.PropertyChanged
public bool DisplayPopup { get; set; }

public MainViewModel() : base()
{
TankItems = new ObservableCollection<ContactViewModel>()
{
new ContactViewModel(),
new ContactViewModel()
};
}
}

MainWindow.xaml

<ListBox ItemsSource="{Binding TankItems}" 
SelectedItem="{Binding SelectedTankItem}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected"
Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type ContactViewModel}">
<StackPanel>
<TextBlock Text="Test" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

<Border>
<TextBlock Text="{Binding DisplayPopup}" />
</Border>

WPF TextBox Not Updating with Data Binding, iNotifyPropertyChanged, and PropertyChanged Trigger

I'm trying to understand why using the ref keyword in your viewModel. I learned a nice way to create the BaseViewModel from the Classon and Baxter book which you can find below. The view-model implements the INotifyPropertyChanged like you did. What you did with [CallerMemberName] is great, it's really magical the way we can reference to our properties thanks to it.

The view model uses a the dictionary to store its properties. It uses a pretty neat trick of looking through the dictionnary keys to see if we contain the string name of the property.Otherwise, we will return a default T value.

 public class CommonBaseViewModel: INotifyPropertyChanged
{
private Dictionary<string, object> Values { get; set; }

protected CommonBaseViewModel()
{
this.Values = new Dictionary<string, object>();
}

public event PropertyChangedEventHandler PropertyChanged;

protected T GetValue<T>([CallerMemberName] string name=null)
{
if (this.Values.ContainsKey(name))
{
return (T)this.Values[name];
}
else
{
return default(T);
}
}

protected void SetValue(object value, [CallerMemberName] string name = null)
{
this.Values[name] = value;

//notify my property
this.OnPropertyChanged(new PropertyChangedEventArgs(name));

}

protected void OnPropertyChanged([CallerMemberName] string name=null)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(name));
}

protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if(this.PropertyChanged != null)
{
this.PropertyChanged(this, e);
}
}
}

As for your GenerateReportViewModel, with the common view model that I provided you, your class then becomes :

   public class GenerateReportsViewModel : CommonViewModelBase
{

private string _parameterFileSelected;
public string parameterFileSelected
{
get { return _parameterFileSelected; }
set { SetValue(ref _parameterFileSelected, value); }
}
get
{
return this.GetValue<string>();
}
set
{
this.SetValue(value);
}

public void updateParameterFileSelected(string parameterFile)
{
parameterFileSelected = parameterFile;
}

}

Oh before I forgot, I don't know if it was your intention, but your GenerateReportViewModel is private. This has some impact on your code. Don't forget that by defaut, classes are private!

As for your code behind, even though it could be consider bad practice, I recommend that you have a private field (OpenFileDialog _openFileDialog)that you construct while initializing your page. Because doing it each time your clicking your button is going to consume more data that you need your application to.

//EDIT
I have review my code,and it seemed that the property was not programmed correctly.
public class GenerateReportsViewModel : CommonViewModelBase
{

               private string _parameterFileSelected;
public string parameterFileSelected
{
get
{
return this.GetValue<string>();
}
set
{
this.SetValue(value);
}

public void updateParameterFileSelected(string parameterFile)
{
parameterFileSelected = parameterFile;
}

}

More about my comment about constructing the page and binding the view model. While creating your page, you have to create the view-model for that page and then bind it to the data context.
I don't know what you do in your code, but I could provide with this sample such as

public GenerateReportView()
{
InitializeComponent();
//Some operations
var generateReportViewModel = new GenerateReportViewModel();
this.DataContext = generateReportViewModel;
}

c# Why is my Databinding not updating an PropertyChanged Event?

You didn't set the DataContext.

In your constructor write:

public MainWindow()
{
InitializeComponent();
DataContext = this;
}

this enables your controls to listen for property changed events triggered by your MainWindow (the DataContext)

WPF Databinding from custom property not updating

Your problem is you're not notifying UI that Fullname property has changed. Technically it has not changed, but it is dependent on some other property.

The simplest solution here is to raise NotifyProperty event for all properties. You won't have to write plenty of code for it, just pass null in NotifyPropertyChanged();

private Data.Patient patient;

public Data.Patient Patient
{
get { return patient; }
set
{
patient = value;
NotifyPropertyChanged(null); //informs view that all properties updated
}
}

It will tell your model that all properties have been updated.

MSDN:

The PropertyChanged event can indicate all properties on the object
have changed by using either null or String.Empty as the property name
in the PropertyChangedEventArgs.

WPF Binding is correct, but window does not update

Your properties need to raise the PropertyChanged event when they are changed. Your class should implement INotifyPropertyChanged

/// <inheritdoc cref="INotifyPropertyChanged"/>
public partial class YourViewModel : INotifyPropertyChanged
{
/// <inheritdoc/>
public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Notifies that a properties value has changed.
/// </summary>
/// <param name="propertyName">The property that has changed.</param>
public virtual void NotifyPropertyChanged([CallerMemberName]string propertyName = null)
{
this.CheckForPropertyErrors();

this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

and then your properties should look like this:

    private QuestionViewModel currentQuestion;
public QuestionViewModel CurrentQuestion
{
get
{
return this.currentQuestion;
}

set
{
this.currentQuestion = value;
this.NotifyPropertyChanged();
}
}


Related Topics



Leave a reply



Submit