Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 已激发PropertyChanged事件,但未调用属性获取程序_C#_Wpf_Xaml - Fatal编程技术网

C# 已激发PropertyChanged事件,但未调用属性获取程序

C# 已激发PropertyChanged事件,但未调用属性获取程序,c#,wpf,xaml,C#,Wpf,Xaml,我遇到了一个问题,PropertyChanged事件正在触发,但是UI没有更新,并且没有调用相关的属性getter。在其他SO帖子中,我看到当视图中没有任何内容实际绑定到属性时,人们会遇到这个问题,但我确实绑定了一些内容 下面是一个例子,每当在日期选择器中更改新的出生日期时,我都会尝试更新“年龄”输出。相关代码,我已经尽可能地简化了: 型号: public class Pft { public DateTime Dob; } 视图模型: public class ViewModelBa

我遇到了一个问题,PropertyChanged事件正在触发,但是UI没有更新,并且没有调用相关的属性getter。在其他SO帖子中,我看到当视图中没有任何内容实际绑定到属性时,人们会遇到这个问题,但我确实绑定了一些内容

下面是一个例子,每当在日期选择器中更改新的出生日期时,我都会尝试更新“年龄”输出。相关代码,我已经尽可能地简化了:

型号:

public class Pft {
    public DateTime Dob;
}
视图模型:

public class ViewModelBase : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class PftViewModel : ViewModelBase {
    private Pft pft;

    public DateTime Dob {
        get { return pft.Dob; }
        set {
            pft.Dob = value;
            NotifyPropertyChanged();
            DemographicsChanged();
        }
    }

    public int Age {
        get {  // << this does not get called after NotifyPropertyChanged("Age")
            DateTime today = DateTime.Today;

            int age = today.Year - Dob.Year;

            if (Dob > today.AddYears(-age)) {
                age--;
            }

            return age;
        }
    }

    private void DemographicsChanged() {
        NotifyPropertyChanged("Age");  // << this gets called!
    }

    public PftViewModel() {
        pft = new Pft();
    }
}

public class EntryPageViewModel : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private PftViewModel _pft;
    public PftViewModel Pft {
        get { return _pft; }
        set {
            _pft = value;
            NotifyPropertyChanged();
        }
    }

    public EntryPageViewModel() {
        Pft = new PftViewModel();
    }
}

那代码对我有用。当我更改
Dob
时,它会在UI中更新
Age
<代码>模式=单向在
文本块上是不必要的。顺便说一下,Text
。您只需要在
Run.Text
上使用它。它也适用于我。@JssDWt他正在使用
CallerMemberNameAttribute
,这很好。他需要指定该参数的情况是在
demographicshanged()
中调用
“Age”
,他在那里传递了该参数。@EdPlunkett-Hmm,谢谢。我为了简化代码而删掉的其他东西一定把它弄糟了。@AndrewEwert这是我的猜测。但现在很简单:只要把东西放回去,直到它破裂。
<Page x:Class="EntryPage">
    <StackPanel>
        <DatePicker SelectedDate="{Binding Pft.Dob}" />
        <TextBlock Text="{Binding Pft.Age, Mode=OneWay}" />
    </StackPanel>
</Page>
public partial class EntryPage : Page {
    private EntryPageViewModel viewModel;

    public EntryPage() {
        InitializeComponent();

        viewModel = new EntryPageViewModel();
        DataContext = viewModel;
    }
 }