3
votes

I have a custom class Spieltag containing a property SpieltagDaten (custom class). SPieltagDaten has a property called SpieleGespielt. I have set this property as target of a binding on textbox text-property.

The binding works just fine as the initial value of the property is beeing displayed but when the property changes the textbox is not beeing updated.

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

This is what the code looks like:

This is the textbox with binding:

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

In constructor of the window I set

this.DataContext = this;

This is the root-class of the model containing a property called Daten:

public class Spieltag : ModelBase    {

    private ISpieltagDaten daten;

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

SpieltagDaten contains a property SpieleGespielt

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:

I forget to post the Converter. It's derived from MarkupExtension so I can use it easy in 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();
    }
}

The converter is beeing called on the initial setup of the model. enter image description here

1
Are you sure that converter is working fine?Haris Hasan
Try putting a breakpoint inside that converter. If it is not hit when property change is raised, the binding does not work.decyclone
Yes the converter is working fine, it's called when the initial values are set but it's not called when I change model values.chaosr

1 Answers

2
votes

your Converter is not specified correctly. Specify an instance of your converter class in the UserControl.Resources and then reference that instance in your 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}" />