mvvmgen icon indicating copy to clipboard operation
mvvmgen copied to clipboard

Updating property does not reflect bound TextBox in Windows App SDK.

Open hayashida-katsutoshi opened this issue 4 years ago • 1 comments
trafficstars

  • MvvmGen 1.1.2
  • Windows App SDK (Experimental) 1.0.0.50489432
  • VisualStudio 2019 16.11.5

I am starting to use MVVMgen. Thank you for sharing great library, I like it very much. Now I am probably doing something wrong and I am not able to figure it out what is happening. I have limited experience on XAML.

A text box is binding to a property in a view model in the sample below. When you press the button and select a folder, the model view updates the property, but text box on the window does not update.

XAML

<StackPanel>
    <TextBox Header="Local Path" Margin="0,0,0,16" Text="{Binding LocalPath, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Content="Choose Folder" Margin="0,0,0,16" Command="{Binding SelectLocalPathCommand}"/>
</StackPanel>

View

namespace MVVM_TextBox
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            DataContext = new ViewModels.MainPageViewModel();
        }
    }
}

Model

namespace MVVM_TextBox.ViewModels
{
    [ViewModel]
    public partial class MainPageViewModel
    {
        [Property] string _localPath = "";

        [Command]
        private async void SelectLocalPath()
        {
            var picker = new FolderPicker();
            picker.FileTypeFilter.Add("*");
            var folder = await picker.PickSingleFolderAsync();
            if (folder != null)
            {
                LocalPath = folder.Path;
            }
        }
    }
}

g.cs file generated by MVVM gen.

namespace MVVM_TextBox.ViewModels
{
    partial class MainPageViewModel : global::MvvmGen.ViewModels.ViewModelBase
    {
        public MainPageViewModel()
        {
            this.InitializeCommands();
            this.OnInitialize();
        }

        partial void OnInitialize();

        private void InitializeCommands()
        {
            SelectLocalPathCommand = new DelegateCommand(_ => SelectLocalPath());
        }

        public DelegateCommand SelectLocalPathCommand { get; private set; }

        public string LocalPath
        {
            get => _localPath;
            set
            {
                if (_localPath != value)
                {
                    _localPath = value;
                    OnPropertyChanged("LocalPath");
                }
            }
        }
    }
}

Complete project file can be found at: https://github.com/hayashida-katsutoshi/MVVM-TextBox

hayashida-katsutoshi avatar Nov 06 '21 00:11 hayashida-katsutoshi

Not a bug in mvvmgen. It's a feature in WinUI 😄

jh-isw avatar May 29 '22 07:05 jh-isw

Hey @jh-isw , I'm scanning the issues, and I missed your message. Thanks for linking the PR on the other repo.

I was actually trying this without MvvmGen and saw the same issue, but didn't figure out why it happens. Seems x:Bind is always the better option, unless there's a specific reason why you want to use Binding.

thomasclaudiushuber avatar Nov 25 '22 14:11 thomasclaudiushuber

@jh-isw I'm closing this one. If there's anything unclear, please feel free to comment here again or open a new issue. Thank you!

thomasclaudiushuber avatar Nov 25 '22 14:11 thomasclaudiushuber