简体   繁体   English

绑定不适用于自定义 BindableProperty

[英]Binding not working on custom BindableProperty

I'm having trouble binding a value to a custom ContentView I made.我无法将值绑定到我制作的自定义ContentView

My MainPage uses CustomFrameView which is a ContentView .我的MainPage使用CustomFrameView这是一个ContentView

My CustomFrameView has a string BindableProperty .我的CustomFrameView有一个字符串BindableProperty This works fine when I manually enter a string, but fails when I try to use Binding.当我手动输入字符串时,这工作正常,但当我尝试使用绑定时失败。 And I'm sure the binding should work because it works when used on a Label .而且我确信绑定应该可以工作,因为它在Label上使用时可以工作。

Anyone has an idea on why it does not work?任何人都知道为什么它不起作用?

MainPage.xaml主页.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主页.xaml.cs

    public MainPage()
    {
        InitializeComponent();

        BindingContext = new MainPageViewModel();
    }

MainPageViewModel.cs MainPageViewModel.cs

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

CustomFrameView.xaml.cs 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 CustomFrameViewModel.cs

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

例子

It's simple actually, the reason it does not work is that you are setting a different binding context to your control itself.实际上很简单,它不起作用的原因是您正在为控件本身设置不同的绑定上下文。 It usually uses its parent's view's BC.它通常使用其父视图的 BC。 Your Controls are your views and they don't have separate ViewModels.您的控件是您的视图,它们没有单独的视图模型。 You can brush up on your MVVM here: https://docs.microsoft.com/en-us/dotnet/maui/xaml/fundamentals/mvvm您可以在此处复习您的 MVVM: https://docs.microsoft.com/en-us/dotnet/maui/xaml/fundamentals/mvvm

I recommend you change the following:我建议您更改以下内容:

In your Custom control you go something like this:在您的自定义控件中,您的 go 是这样的:

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();
        }
    }

Dump your Custom Control VM and then see if this works out for you.转储您的自定义控制虚拟机,然后看看这是否适合您。

Also if you are using Maui you can check out my custom controls if you wanna learn how to do them properly: https://github.com/FreakyAli/MAUI.FreakyControls此外,如果您使用的是 Maui,如果您想了解如何正确操作,可以查看我的自定义控件: https://github.com/FreakyAli/MAUI.FreakyControls

Good luck!祝你好运!

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

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