[GH-ISSUE #232] Multiple bindings on the same .NET instance, last binding breaks previous ones (help wanted) #148

Open
opened 2026-05-05 11:06:49 -06:00 by gitea-mirror · 0 comments
Owner

Originally created by @walbarm on GitHub (Feb 8, 2021).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/232

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
    }
}
Originally created by @walbarm on GitHub (Feb 8, 2021). Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/232 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](https://github.com/qmlnet/qmlnet/files/5945887/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 } } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/qmlnet#148
No description provided.