简体   繁体   中英

WPF Binding does not update Button-Content

May someone tell me whats wrong with this sourcecode? when i click the button its not updating ist value? At first binding the converter makes his job.

the sourcecode is pretty big so i will post only some snippets.

XAML: Instances is type of ObservableCollection

<ListBox Name="Instances">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Tag="{Binding Path=Instance.Name}" Content="{Binding Path=Instance.Active, Converter={StaticResource BTSC}}" Click="ChangeAccess"/>
            <TextBlock Text="{Binding Path=Instance.Name}"/>
         </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Converter:

public class BoolToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (((Boolean)value) == true)
            return "No";
        else
            return "Yes";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
} 

Event:

private void ChangeAccess(object sender, RoutedEventArgs e)
{
    for...
    if ((sender as Button).Tag.ToString() == (DP.Instances[i].Instance as CInstance).Name)
    {
        SkipIfAndElse...
        DP.Instances[i].Instance.Active = true;
    }
}

CInstance:

class CInstance : INotifyPropertyChanged
{
    private Boolean active;
    public Boolean Active
    {
        get { return active; }
        set
        {
            active = value;
            NotifyPropertyChanged("Access");
        }
    }
}

All other values of the CInstance class are updating as expected.

In your CInstance class

NotifyPropertyChanged("Access");

should be

NotifyPropertyChanged("Active");

I would suggest you start using some kind of INPC framework. I personally like Simon Cropp's Fody . Fody adds the appropriate OnNotifyPropertyChanged as a post compilation step, which means you don't get the Runtime hit that you get with Expression based solutions.

At the end of the day, string based OnNotifyPropertyChanged are all pretty fragile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM