unoplatform / uno

Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
https://platform.uno
Apache License 2.0
8.63k stars 697 forks source link

Top-level Self-binding DataContext in Page is not working #212

Open Daoting opened 5 years ago

Daoting commented 5 years ago

I'm submitting a...

Current behavior

TextBox does not support text binding in iOS. UI is no response when include text binding. <TextBox Text="{Binding Title}" />

Expected behavior

Minimal reproduction of the problem with instructions

Environment

Nuget Package: 

Package Version(s): 

Affected platform(s):
- [x] iOS
- [ ] Android
- [ ] WebAssembly
- [ ] Windows
- [ ] Build tasks

Visual Studio
- [x] 2017 (version: )
- [ ] 2017 Preview (version: )
- [ ] for Mac (version: )

Relevant plugins
- [ ] Resharper (version: )
MatFillion commented 5 years ago

Thank you @Daoting for your report.

Typically I bind the Text property of TextBox using

<TextBox Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

Could you provide your code sample? (XAML and C# DataContext) It would help use understand and reproduce your issue.

Daoting commented 5 years ago

xaml:

<dt:PageWin
    x:Class="Dt.Sample.TestDemo1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:dt="using:Dt.Base">

    <TextBox Text="{Binding Title}" />
</dt:PageWin>

Code:

    public sealed partial class TestDemo1 : PageWin
    {
        public TestDemo1()
        {
            InitializeComponent();
            DataContext = this;
        }
    }

Title's define:

    public partial class PageWin : ContentControl, IWin
    {
        public readonly static DependencyProperty TitleProperty = DependencyProperty.Register(
            "Title",
            typeof(string),
            typeof(PageWin),
           new PropertyMetadata("No Title"));

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
    }

It cannot load the ui that include text binding. Thanks @MatFillion .

jeromelaban commented 5 years ago

There may be an issue with self-DataContext binding. Can you try with a sub-object, or using x:Bind ?

Daoting commented 5 years ago

Yes, sub-object, ElementName, x:Bind are ok. Only self-DataContext binding.