【问题标题】:How to bind C# generated button to XAML MVVM如何将 C# 生成的按钮绑定到 XAML MVVM
【发布时间】:2020-12-14 22:24:11
【问题描述】:

到目前为止,我已经阅读了很多关于绑定的内容,并且我已经查看了我的问题。不幸的是,我偶然发现的问题是关于按钮内容的问题。

我的问题是:

如何将 C# 生成的按钮 (new Button (//CONTENT)) 绑定到我的 XAML,而不是将内容绑定到我已经生成的按钮?

在我的 MVVM 代码中,我有一个属性,该属性包含一个名为 RequestedButton 的值的按钮,但我不知道应该在我的 XAML 中使用哪种标记来绑定此属性。

如果它是ObservableCollection,我确实知道如何绑定它,但如果它是单个属性,我不知道如何绑定它。

所以我的问题(直截了当)是:我应该在我的 XAML 中使用什么样的元素,使用属性RequestedButton 的绑定,使用{Binding RequestedButton}

在我的班级中,我有一个名为RequestedButton 的属性,它包含一个按钮作为值。像这样:

Class Foo
{
   public Button RequestedButton {get; set;}

   public Foo()
   {
       RequestedButton = new Button()//GENERATE BUTTON WITH PROPERTIES IN IT
   }
}

我现在应该使用什么标签来正确显示(使用 Binding)我的 XAML 中的 ABOVE 按钮?

【问题讨论】:

  • 您是否在询问如何将代码中生成的完整 Button 对象绑定到您的 XAML?
  • 完全不知道你在说什么,你的问题标题与问题正文不同
  • RequestedButton 是什么?属性还是命令?如果要将操作绑定到按钮单击,您应该在您的虚拟机上创建一个命令,在您的命令上调用该操作并将按钮命令属性绑定到您的虚拟机上的命令。请更好地解释并粘贴一些代码以更好地理解
  • @Jason 是的...这正是我的意思,但我不完全确定如何尽可能好地描述它。
  • @DOM 你有一个Button RequestedButton = new Button(),现在你想要它在 XAML 中?是吗?

标签: c# xamarin xamarin.forms


【解决方案1】:

查看模型

private Button requestedButton = new Button();
public Button RequestedButton
{
    get
    {
        return requestedButton;
    }
    set
    {
        requestedButton = value;
        NotifyPropertyChanged(nameof(RequestedButton));
    }
}
...

Xaml

<ContentView Content="{Binding RequestedButton}"/>

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2011-01-18
    相关资源
    最近更新 更多