Windows runtime 从xaml中的绑定实例化新对象以用于弹出

Windows runtime 从xaml中的绑定实例化新对象以用于弹出,windows-runtime,winrt-xaml,Windows Runtime,Winrt Xaml,我想要实现的目标在XAML中可能不可能实现。如果这是可能的,那么它可能是由于一个值得了解的XAML特性。如果没有,那么我也学到了一些东西 我有一个按钮弹出按钮,它是绑定到视图模型的数据。视图模型通过get访问器向弹出按钮的内容提供对象的新实例 每次按下按钮时,我都希望弹出按钮显示对象的新实例 问题是:对象只创建一次,每次打开弹出按钮时都会重新显示 ViewModel.cs MainPage.xaml.cs 现在一切正常!但是我想用XAML来做 我曾尝试在ViewModel上实现INotifyPr

我想要实现的目标在XAML中可能不可能实现。如果这是可能的,那么它可能是由于一个值得了解的XAML特性。如果没有,那么我也学到了一些东西

我有一个按钮弹出按钮,它是绑定到视图模型的数据。视图模型通过get访问器向弹出按钮的内容提供对象的新实例

每次按下按钮时,我都希望弹出按钮显示对象的新实例

问题是:对象只创建一次,每次打开弹出按钮时都会重新显示

ViewModel.cs

MainPage.xaml.cs

现在一切正常!但是我想用XAML来做

我曾尝试在ViewModel上实现INotifyPropertyChanged,但没有成功

类ViewModel:INotifyPropertyChanged { 静态int项计数

public Item GetNewItem {
    get {
        itemCount++;
        Debug.WriteLine("Created item: " + itemCount);
        OnPropertyChanged("GetNewItem");
        return new Item() { Id = itemCount, Name = "Item_" + itemCount} ;
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name) {
    var handler = PropertyChanged;
    if (handler != null) {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

我最近发了100分奖金,所以我会推迟这一点。但是,如果我得到了一个好的答案,我会考虑它。甚至当我自己的分数重新建立时,也可以给出它。
    <Page.Resources>
    <local:ViewModel x:Key="ViewModel"/>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
      DataContext="{StaticResource ViewModel}">
    <Button Content="Create Item">
        <Button.Flyout>
            <Flyout>
                <StackPanel DataContext="{Binding Path=GetNewItem}">
                    <TextBlock Text="{Binding Path=Id}"/>
                    <TextBlock Text="{Binding Path=Name}"/>
                </StackPanel>
            </Flyout>
        </Button.Flyout>
    </Button>

</Grid>
private void Flyout_Opening(object sender, object e) {
    var gridDataContext = (ViewModel)this.grid.DataContext;
    this.stackPanel.DataContext = gridDataContext.GetNewItem;
}
public Item GetNewItem {
    get {
        itemCount++;
        Debug.WriteLine("Created item: " + itemCount);
        OnPropertyChanged("GetNewItem");
        return new Item() { Id = itemCount, Name = "Item_" + itemCount} ;
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name) {
    var handler = PropertyChanged;
    if (handler != null) {
        handler(this, new PropertyChangedEventArgs(name));
    }
}