简体   繁体   English

WPF绑定不会更新Button-Content

[英]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? 当我点击按钮它没有更新ist值? 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 XAML:Instances是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: 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. CInstance类的所有其他值都按预期更新。

In your CInstance class 在您的CInstance类中

NotifyPropertyChanged("Access");

should be 应该

NotifyPropertyChanged("Active");

I would suggest you start using some kind of INPC framework. 我建议你开始使用某种INPC框架。 I personally like Simon Cropp's Fody . 我个人很喜欢西蒙Cropp的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. Fody将相应的OnNotifyPropertyChanged添加为后编译步骤,这意味着您无法获得基于Expression的解决方案所获得的运行时命中。

At the end of the day, string based OnNotifyPropertyChanged are all pretty fragile. 在一天结束时,基于字符串的OnNotifyPropertyChanged都非常脆弱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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