c# - 绑定(bind)不适用于自定义 BindableProperty

标签 c# xaml mvvm maui .net-maui

我无法将值绑定(bind)到我创建的自定义 ContentView

我的 MainPage 使用 CustomFrameView,它是一个 ContentView

我的 CustomFrameView 有一个字符串 BindableProperty。这在我手动输入字符串时工作正常,但在我尝试使用 Binding 时失败。而且我确信绑定(bind)应该有效,因为它在 Label 上使用时有效。

有人知道为什么它不起作用吗?

MainPage.xaml

        <StackLayout> 
    <!-- DOES work -->
          <Label Text="{Binding Name}"/>
    <!-- DOES work-->
          <controls:CustomFrameView TestName="Test456"/>
    <!-- DOES NOT work-->
          <controls:CustomFrameView TestName="{Binding Name}"/>
        </StackLayout>

MainPage.xaml.cs

    public MainPage()
    {
        InitializeComponent();

        BindingContext = new MainPageViewModel();
    }

MainPageViewModel.cs

    public partial class MainPageViewModel : ObservableObject
    {
        [ObservableProperty]
        private string name;
    
        public MainPageViewModel ()
        {
            Name = "Test123";
        }
    }

CustomFrameView.xaml.cs

    public partial class CustomFrameView : ContentView
    {
    public string TestName
        {
            get => (string)GetValue(TestNameProperty);
            set => SetValue(TestNameProperty, value);
        }
    
        public static readonly BindableProperty TestNameProperty =
            BindableProperty.Create(
                nameof(TestName),
                typeof(string),
                typeof(CustomFrameView),
                "",
                propertyChanged: SetTestNameProperty);
    
    
        private static void SetTestNameProperty(BindableObject bindable, object oldValue, object newValue)
        {     
            var vm = bindable.BindingContext as CustomFrameViewModel;
            vm.TestName = (string) newValue;
        }
    
        public CustomFrameView()
        {
            InitializeComponent();
            this.BindingContext = new CustomFrameViewModel();
        }
    }

CustomFrameViewModel.cs

    public partial class CustomFrameViewModel : ObservableObject
    {
        [ObservableProperty]
        private string testName;
    }

Example

最佳答案

其实很简单,它不起作用的原因是您为控件本身设置了不同的绑定(bind)上下文。它通常使用其父 View 的 BC。您的控件是您的 View ,它们没有单独的 ViewModel。您可以在此处重温您的 MVVM:https://learn.microsoft.com/en-us/dotnet/maui/xaml/fundamentals/mvvm

我建议您更改以下内容:

在您的自定义控件中,您可以执行以下操作:

public partial class CustomFrameView : ContentView
    {
    public string TestName
        {
            get => (string)GetValue(TestNameProperty);
            set => SetValue(TestNameProperty, value);
        }
    
        public static readonly BindableProperty TestNameProperty =
            BindableProperty.Create(
                nameof(TestName),
                typeof(string),
                typeof(CustomFrameView),
                string.Empty;
                );
    
    
        public CustomFrameView()
        {
            InitializeComponent();
        }
    }

转储您的自定义控制 VM,然后看看这是否适合您。

此外,如果您正在使用 Maui,如果您想了解如何正确使用它们,可以查看我的自定义控件:https://github.com/FreakyAli/MAUI.FreakyControls

祝你好运!

关于c# - 绑定(bind)不适用于自定义 BindableProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73691579/

相关文章:

c# - Entity Framework 无法在实体构造函数中绑定(bind)值对象

c# - 如何将文字方程式转换为图像?

c# - WPF MVVM DataGrid 过滤使用组合框

WPF:如何在按钮单击时关闭对话框

WPF - 基于内容突出显示文本框

c# - 使用 C# 的 Asp.Net 中的文本框?

c# - Entity Framework ;如何处理foreach循环中的异常并不断迭代

c# - 编辑现有 WP8 项目时出错

c# - WPF MVVM 将 TextBlock 的文本绑定(bind)到 ObservableCollection 成员

c# - 在 Prism 中处理 PreviewMouseDown 和 PreviewMouseUp 事件