Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

xaml - Can't create Bindable Property for custom ContentView

I have created a ContentView with a single Label (I plan to add more later).

PageHeadingView.xaml

  <ContentView.Content>
        <StackLayout Orientation="Vertical" BackgroundColor="Red">
            <Label x:Name="HeadingLabel"  Text="{Binding HeadingText}" />
        </StackLayout>
  </ContentView.Content>

I defined a BindableProperty in my code behind. I also set the BindingContext of my view to be itself.

PageHeadingView.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PageHeadingView : ContentView
{
    public PageHeadingView()
    {
        InitializeComponent();
        this.BindingContext = this;
    }

    public static readonly BindableProperty HeadingTextProperty = BindableProperty.Create(nameof(HeadingText), typeof(string), typeof(PageHeadingView), default(string));
    public string HeadingText { get => (string)GetValue( HeadingTextProperty); set => SetValue(HeadingTextProperty, value); }
}

I then added the View to my ContentPage. I also added a test Label inside a StackLayout to ensure my bindings were working correctly.

HomePage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:views="clr-namespace:MyProject.Views"
             x:Class="MyProject.HomePage">
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <views:PageHeadingView HeadingText="{Binding Name}" />
            <Label Text="{Binding Name}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

And set my BindingContext in code.

HomePage.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class HomePage : ContentPage
{
    public HomePage()
    {
        InitializeComponent();
        //ViewModel contains a string property named: Name;
        this.BindingContext = new ViewModel();
    }
}

When I run my code, my PageHeadingView does not display any text. I can see the red background color, so I know the control has been added to the Page correctly. The test Label I placed in StackLayout also works correctly, and I am able to see the bound value.

What do I need to do to make my CustomView display Bindable content?

question from:https://stackoverflow.com/questions/65928180/cant-create-bindable-property-for-custom-contentview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From your code, you may have some problems when using bindableproperty bidning, I create simple sample that you can take a look:

PageHeadingView.xaml:

 <ContentView.Content>
    <StackLayout BackgroundColor="Red" Orientation="Vertical">
        <Label x:Name="HeadingLabel" />
    </StackLayout>
</ContentView.Content>

PageHeadingView.cs, you can update HeadingText value by HeadingTextPropertyChanged, then display HeadingLabel.

 public partial class PageHeadingView : ContentView
{

    public static BindableProperty HeadingTextProperty= BindableProperty.Create(
                                                     propertyName: "HeadingText",
                                                     returnType: typeof(string),
                                                     declaringType: typeof(PageHeadingView),
                                                     defaultValue: "",
                                                     defaultBindingMode: BindingMode.OneWay,
                                                     propertyChanged: HeadingTextPropertyChanged);
    
    public string HeadingText
    {
        get { return base.GetValue(HeadingTextProperty).ToString(); }
        set { base.SetValue(HeadingTextProperty, value); }
    }
    private static void HeadingTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (PageHeadingView)bindable;
        control.HeadingLabel.Text = newValue.ToString();
    }

    public PageHeadingView()
    {
        InitializeComponent();
    }
}

<StackLayout>
        <customcontrol:PageHeadingView HeadingText="{Binding Name}" />
        <Label Text="{Binding Name}" />
    </StackLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...