ImageSource doesn't change - c#

My XAML:
<Button Click="LikePost" BorderThickness="0" >
<Image Stretch="Uniform" Source="{Binding imagesource}" />
</Button>
Setting the imagesource for the first time works as expected but whenever I update the source string in my code the XAML does not update, and yes I have included INotifyPropertyChanghed:
public class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _imagesource;
public string imagesource
{
get { return _imagesource; }
set
{
if (_imagesource == value) return;
_imagesource = value;
NotifyLikeImageChanged("like");
}
}
private void NotifyLikeImageChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
What I'm I doing wrong?

But you're sending the wrong property name, change this:
NotifyLikeImageChanged("like");
to this:
NotifyLikeImageChanged("imagesource");

Related

OnPropertyChanged() not working in MVVM

I searched through all other question with the same problem. But I can't find any solution from them.
OnPropertyChanged is firing but the Control is not updating. I'm using the Mahapps.Metro ProgressRing Control.
View Code
<controls:MetroWindow.Resources>
<userObj:LoginViewLogic x:Key="UserData"/>
<userObj:LoginViewLogic x:Key="LoginViewLogic"/>
</controls:MetroWindow.Resources>
<Grid>
<Canvas>
<controls:ProgressRing Name="ProgressRing" Canvas.Left="133" Canvas.Top="154" Height="50" Width="35" IsActive="{Binding Source={StaticResource UserData},Path=UserData.IsProgressRingActive}"/>
</Canvas>
</Grid>
ViewModel Code
public class LoginViewLogic {
public LoginViewLogic() {
_userData = new User(AppSettings.ReadCredentials(),(bool)loadedSettings);
}
private User _userData;
public User UserData
{
get { return _userData; }
set { _userData = value; }
}
public async void Login() {
_userData.IsProgressRingActive = true;
var loginResult = await Stuff.Login(_userData);
if (!loginResult) {
MessageBox.Show("You have entered an invalid username or password",
"Information", MessageBoxButton.OK, MessageBoxImage.Error);
_userData.IsProgressRingActive = false;
}
}
Model Code
public class User : INotifyPropertyChanged {
private bool _isProgressRingActive;
public bool IsProgressRingActive {
get { return _isProgressRingActive; }
set {
_isProgressRingActive = value;
OnPropertyChanged("IsProgressRingActive");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Have you tried this:
<controls:MetroWindow.Resources>
<userObj:LoginViewLogic x:Key="LoginViewLogic"/>
</controls:MetroWindow.Resources>
<Grid>
<Canvas>
<controls:ProgressRing Name="ProgressRing" Canvas.Left="133" Canvas.Top="154" Height="50" Width="35" IsActive="{Binding Source={StaticResource LoginViewLogic},Path=UserData.IsProgressRingActive}"/>
</Canvas>
</Grid>
So you are only creating and binding to one instance of LoginViewLogic class?
Your viewmodel should also raise a PropertyChanged event.
Try with below code.
public class LoginViewLogic : INotifyPropertyChanged
{
public LoginViewLogic()
{
_userData = new User(AppSettings.ReadCredentials(),(bool)loadedSettings);
}
private User _userData;
public User UserData
{
get { return _userData; }
set { _userData = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

Textblock binding text

I want to display the string _lunedi in a textbox, the string _lunedi is updated periodically using InitializeBoxes().
I have my class:
public event PropertyChangedEventHandler PropertyChanged;
private string _lunedi= "lunedi ";
public string lunedi
{
get { return this._lunedi; }
set
{
if (value != this._lunedi)
{
this._lunedi = value;
NotifyPropertyChanged("lunedi");
}
}
}
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
whit this method change the lunedi:
private void InitializeBoxes()
{
lunedi += todayPivot.Day;
}
xaml:
<Border>
<TextBlock Text="{Binding Path=lunedi, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
</Border>
The problem is that the text of the textblock is empty.
Thank you.
Sounds like the binding is using one time try this instead
<Border>
<TextBlock Text="{Binding Path=lunedi, Mode=OneWay}"></TextBlock>

Bindings on a ContentControl do not update the data

I have the code:
<TextBox Width="200" Text="{Binding Value}"></TextBox>
Which works. However the "Value" can be different types. So if I have an bool I want to show a checkbox. I rewrote it as as follow, which kinda works:
<ContentControl Content="{Binding Value}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type sys:Boolean}">
<CheckBox IsChecked="{Binding Path=.}"></CheckBox>
</DataTemplate>
<DataTemplate DataType="{x:Type sys:Double}">
<TextBox Width="200" Text="{Binding Path=.}"></TextBox>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
But now the property isn't updated like before. I have tried setting Mode=Twoway, but it still do not work.
Edit
It worked perfectly fine when I only had the textbox, editing the text of the textbox updated the model. However when I tried doing this with the second code (ContentControl) it just doesn't work.
Code
I'm using Mvvm-light togheter with bindings. The "Value" is bound to an instance of Property
[JsonObject]
public class Property<T> : INotifyPropertyChanged
{
[JsonProperty]
public String name;
public Property(String name, T value)
{
this._value = value;
this.name = name;
}
[JsonIgnore]
public T Value {
get { return _value; }
set {
_value = value;
hot = true;
NotifyPropertyChanged("Value");
}
}
[JsonProperty(PropertyName = "value")]
private T _value;
[JsonIgnore]
public String Name { get { return name; } set { name = value; } }
[JsonProperty]
public bool hot = false;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
You should implement INotifyPropertyChanged interface in order to track property changes. I'm sure everything works fine then.
This works for me:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private object value;
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
DataContext = this;
}
public object Value
{
get { return value; }
set
{
this.value = value;
NotifyPropertyChanged("Value");
}
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Value = true;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}

Silverlight ComboBox SelectedValue TwoWay Binding not working

I have used many ComboBoxes in my applications and all of them are working without any problem. But, I can't find the problem now. I have set SelectedValuePath to "Tag" property. But the property not updating after changing the ComboBox selected item. I have read other StackOverflow questions, but nontheless helped.
It is xaml:
xmlns:vms="clr-namespace:SilverlightApplication1"
<UserControl.DataContext>
<vms:MainViewModel />
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" Background="White">
<ComboBox Width="100" VerticalAlignment="Center" FontFamily="Segoe UI"
Height="30" Margin="0,5,0,0" HorizontalAlignment="Left"
SelectedValue="{Binding SelectedDifStatusComparer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Tag">
<ComboBox.Items>
<ComboBoxItem Tag="H" >High</ComboBoxItem>
<ComboBoxItem Tag="L" >Low</ComboBoxItem>
<ComboBoxItem Tag="E" >Equal</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
</Grid>
And here is the ViewModel:
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _selectedDifStatusComparer = "";
private string SelectedDifStatusComparer
{
get { return _selectedDifStatusComparer; }
set
{
_selectedDifStatusComparer = value;
MessageBox.Show(_selectedDifStatusComparer);
OnPropertyChanged("SelectedDifStatusComparer");
}
}
public MainViewModel()
{
SelectedDifStatusComparer = "E"; // It is working, the MessageBox is apperaing
}
}
Your property is private. Change it to public and it should work.
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _selectedDifStatusComparer = "";
public string SelectedDifStatusComparer
{
get { return _selectedDifStatusComparer; }
set
{
_selectedDifStatusComparer = value;
MessageBox.Show(_selectedDifStatusComparer);
OnPropertyChanged("SelectedDifStatusComparer");
}
}
public MainViewModel()
{
SelectedDifStatusComparer = "E"; // It is working, the MessageBox is apperaing
}
}
Your property is private. Change it to public and it should work.

Binding drives me Crazy

Edit:
the XAML:
<TextBlock Text="{Binding Path=CurrentShows}" Grid.Row="1"/>
produces following Output:
SilverlightHelloWorld.Deserialize.schedule
the XAML:
<TextBlock Text="{Binding Path=CurrentShows.Today}" Grid.Row="1"/>
nor
<TextBlock Text="{Binding Path=CurrentShows.Today.Date}" Grid.Row="1"/>
produce any Error or ouput while being in debugmode.
Anysuggestions?
OldStatement:
I've got a quiet well complicated Example here,
the best will be, I am starting with my Code.
This is my Codebehind:
Mainpage.xaml.cs
schedule currentshows;
public schedule CurrentShows
{
protected set
{
if (currentshows != value)
{
currentshows = value;
OnPropertyChanged("CurrentShows");
}
}
get
{
return currentshows;
}
}
schedule.cs
[XmlRoot]
public class schedule : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
DayCollection today;
[XmlElement("DAY")]
public DayCollection Today
{
set
{
if (today != value)
{
today = value;
OnPropertyChanged("Today");
}
}
get
{
return today;
}
}
private void OnPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
DayCollection.cs
public class DayCollection : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
ObservableCollection<TimeCollection> timecol;
[XmlElement("time")]
public ObservableCollection<TimeCollection> TimeCol
{
set
{
if (timecol != value)
{
timecol = value;
OnPropertyChanged("TimeCol");
}
}
get
{
return timecol;
}
}
string date;
[XmlAttribute(AttributeName = "attr")]
public string Date
{
set
{
if (date != value)
{
date = value;
OnPropertyChanged("Date");
}
}
get
{
return date;
}
}
private void OnPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
I am trying to get the String-Property "Date" in my Xaml to Show-Up.
But I am just don't get any Solution.
I totally would appreciate any help here, Thanks in Advance !
I've copied your code into a project and it works if you use this line instead to set up the TextBlock's binding
<TextBlock Text="{Binding Today.Date}" Grid.Row="1"/>
but I cannot tell from your code how you are setting up the outer data binding. Here's what I included in the MainPage constructor
currentshows = new schedule();
currentshows.Today = new DayCollection();
currentshows.Today.Date = "hallo";
LayoutRoot.DataContext = currentshows;
where LayoutRoot is the parent of the TextBlock, i.e.
<Grid x:Name="LayoutRoot">
<TextBlock Text="{Binding Today.Date}"/>
</Grid>

Categories

Resources