question

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 asked EmonHaque-1485 commented

How to update the converted value with IValueConverter without property change

The problem is in Edit Transaction view. For the proper update of transactions, ListBox, I've added an additional string property, TenantName, in the Transaction model and when I update the Tenant's name, I fire an event NameChanged in the EditTenant viewmodel and Transaction view model is a subscriber of that event and does this to update the TenantName:

 EditTenantVM.NameChanged += onTenantNameChanged;
 void onTenantNameChanged(Tenant t) {
     if (transactions == null || transactions.Count == 0) return;
     foreach (var item in transactions) {
         if(item.TenantId == t.Id) {
             item.TenantName = t.Name;
             item.OnPropertyChanged(nameof(Transaction.TenantName));
         }
     }
 }

it works fine. Now, I've another event in EditPlot viewmodel and it's fired when plot's Name changes. Transaction view model is a subscriber to that as well BUT Transaction model doesn't have any string PlotName property, it's int? PlotId property and that is set in the group description:

 Editables.GroupDescriptions.Add(new PropertyGroupDescription(nameof(Transaction.PlotId)));

in the ControlTemplate, I've used converter to convert the PlotId to Name:

 plotName.SetBinding(Run.TextProperty, new Binding(nameof(GroupItem.Name)) { 
     Mode = BindingMode.OneWay, 
     Converter = App.convert.plotId2plotName 
 });

in the handler or Plot's NameChanged I've tried with these in Transaction view model:

 EditPlotVM.NameChanged += onPlotNameChanged;
 void onPlotNameChanged(Plot p) {
     if (transactions == null || transactions.Count == 0) return;
     foreach (var item in transactions) {
         if (item.PlotId == p.Id) {
             item.PlotId = p.Id; // PlotId actually didn't change, only the Name changed
             //item.OnPropertyChanged(string.Empty);
             item.OnPropertyChanged(nameof(Transaction.PlotId));
         }
     }
 }

BUT it doesn't update the Name in Transaction view!

97957-test.gif

Is it possible, somehow, to force it to call the IValueConverter, App.convert.plotId2plotName , to get the updated value?


windows-wpf
test.gif (2.0 MiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@EmonHaque-1485

Could you force to refresh the plotName Binding when it load ?

 plotName.SetBinding(Run.TextProperty, new Binding(nameof(GroupItem.Name))
             {
                 UpdateSourceTrigger=UpdateSourceTrigger.Explicit,
                 Mode = BindingMode.OneWay,
                 Converter = App.convert.plotId2plotName
             });
             plotName.Loaded += PlotName_Loaded;
 private void PlotName_Loaded(object sender, RoutedEventArgs e)
         {
             ((Run)sender).GetBindingExpression(Run.TextProperty)
                      .UpdateTarget();
         }
1 Vote 1 ·

@DaisyTian-MSFT, looks like in the ControlTemplate where plotName is var plotName = new FrameworkElementFactory(typeof(Run)), I can't do +=! Instead I did this in the controlTemplate:

 plotName.AddHandler(Run.LoadedEvent, new RoutedEventHandler(handler));
 ...
 void handler(object sender, RoutedEventArgs e) {
     ((Run)sender).GetBindingExpression(Run.TextProperty).UpdateTarget();
 }

but it doesn't work!

0 Votes 0 ·

0 Answers