Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple bindings on the same .NET instance, last binding breaks previous ones (help wanted) #232

Open
walbarm opened this issue Feb 8, 2021 · 0 comments

Comments

@walbarm
Copy link

walbarm commented Feb 8, 2021

I have 2 TextFields on 2 different QML (tabs) that bound to 2 different properties on the same .NET instance. When the properties get updated, the TextField on the first tab does not get updated. Here is a sample code:
QmlNetTest.zip

TestNetType.cs

public class TestNetType : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    private string _name;
    public string Name
    {
        get => _name;
        set
        {
            if(_name != value)
            {
                _name = value;
                OnPropertyChanged();
            }
        }
    }

    private string _value;
    public string Value
    {
        get => _value;
        set
        {
            if (_value != value)
            {
                _value = value;
                OnPropertyChanged();
            }
        }
    }
}

Program.cs

class Program
{
    public static TestNetType TestNetType { get; } = new TestNetType() { Name = "Test Name", Value = "Test Value" };

    static int Main(string[] args)
    {
        try
        {
            RuntimeManager.DiscoverOrDownloadSuitableQtRuntime();
            QQmlApplicationEngine.ActivateMVVMBehavior();

            using (var application = new QGuiApplication(args))
            using (var qmlEngine = new QQmlApplicationEngine())
            {
                Qml.Net.Qml.RegisterType<TestNetType>("DotNetViewModels");
                Qml.Net.Qml.RegisterType<ViewController>("DotNetViewModels");
                qmlEngine.Load("./Views/MainView.qml");
                return application.Exec();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex);
            return -1;
        }
    }
}

ViewController.cs

public class ViewController
{
    public TestNetType TestNetType => Program.TestNetType;

    private static int _count;

    public void Update()
    {
        _count++;
        TestNetType.Name = $"Test Name {_count}";
        TestNetType.Value = $"Test Value {_count}";
    }
}

MainView.qml

ApplicationWindow {
    id: window
    width: 400
    height: 300
    visible: true
    title: "Qml.Net Test"

    ViewController { id: controller }

    StackLayout {
        id: stackLayout
        anchors.fill: parent

        Tab1View { }
        Tab2View { }
    }

    Row {
        spacing: 12
        width: parent.width
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 8

        TabButton { text: qsTr("Tab 1"); checked: true; onClicked: stackLayout.currentIndex = 0; }
        TabButton { text: qsTr("Tab 2"); onClicked: stackLayout.currentIndex = 1; }
        Button { text: qsTr("Update"); onClicked: controller.update(); }
    }
}

Tab1View.qml

Item {
    property TestNetType test;

    ViewController {
        id: controller

        Component.onCompleted: {
            test = controller.testNetType;
        }
    }

    TextField {
        anchors.horizontalCenter: parent.horizontalCenter
        y: 12
        width: 200
        text: test.name
        onEditingFinished: test.name = text
    }
}

Tab2View.qml

Item {
    property TestNetType test;

    ViewController {
        id: controller

        Component.onCompleted: {
            test = controller.testNetType;
        }
    }

    TextField {
        anchors.horizontalCenter: parent.horizontalCenter
        y: 12
        width: 200
        text: test.value
        onEditingFinished: test.value = text
    }
}
@walbarm walbarm changed the title Multiple bindings on the same .NET instance, last binding breaks previous ones Multiple bindings on the same .NET instance, last binding breaks previous ones (help wanted) Feb 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant