WPF: binding to user class. PropertyChanged started, but view is not updated

I have my own Spieltag class containing the SpieltagDaten property (custom class). SPieltagDaten has the SpieleGespielt property. I set this property as the target of binding the textbox textbox property.

Binding works very well because the initial value of the property is displayed, but when the property changes, the text field is not updated.

The PropertyChanged event is fired and the parameter is spelled correctly.

This is what the code looks like:

This is a binding text box:

<TextBlock Name="textBlockGespielt" 
    Text="{Binding Path=MySpieltag.Daten, 
           Converter={util:SpieltagDatenToStringConverter},
           ConverterParameter=SpieleGespielt, 
           UpdateSourceTrigger=PropertyChanged}" />

In the window constructor, I installed

this.DataContext = this;

This is the root class of the model containing the Daten property:

public class Spieltag : ModelBase    {

    private ISpieltagDaten daten;

    public ISpieltagDaten Daten
    {
        get { return daten; }
        set { daten = value; Changed("Daten"); }
    }
}

SpieltagDaten contains the SpieleGespielt property

public class SpieltagDaten : ModelBase, interfaces.ISpieltagDaten
{
    private int _spieleGespielt;

    public Int32 SpieleGespielt
    {
        get { return _spieleGespielt; }
        set { _spieleGespielt = value; Changed("SpieleGespielt"); }
    }
}

ModelBase implements INotifyPropertyChanged:

public class ModelBase: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    protected void Changed(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

edit:

. MarkupExtension, xaml:

class SpieltagDatenToStringConverter : MarkupExtension, IValueConverter
{
    private static SpieltagDatenToStringConverter _converter = null;
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (_converter == null)
        {
            _converter = new SpieltagDatenToStringConverter();
        }
        return _converter;
    }

    public object Convert(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        ISpieltagDaten daten = (ISpieltagDaten)value;

        string res = String.Empty;

        string propertyName = parameter.ToString();


            PropertyInfo pi = typeof(ISpieltagDaten).GetProperty(propertyName);

            if (pi != null)
            {
                 res = pi.GetValue(daten,null).ToString();
            }

        return res;
    }


    public object ConvertBack(object values, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

. enter image description here

+3
1

. UserControl.Resources, TextBlock

<UserControl.Resources>
    <util:SpieltagDatenToStringConverter x:Key="mySpieltagDatenToStringConverter" />
</UserControl.Resources>

<TextBlock Name="textBlockGespielt" 
    Text="{Binding Path=MySpieltag.Daten, 
           Converter={StaticResource mySpieltagDatenToStringConverter},
           ConverterParameter=SpieleGespielt, 
           UpdateSourceTrigger=PropertyChanged}" />
+2

All Articles