Access Custom Class Variables in a ViewModel through binding

android data binding class not generated
android databinding
view binding class not generated android
android viewmodel with binding
android data binding onclick example
android data binding viewmodel
databindingutil.inflate kotlin
binding viewmodel viewmodel

What I want to do is to be able to access an object's properties through a bound textblock that is found in a viewModel class. So say I want to access the OrderID of the Orders class in a bound textblock that is stored in a viewModel class. What I have for that is:

<textblock text="{Binding Path=Order.OrderID}"/>

This is connected to the order's orderID as when I change the OrderID in the ViewModel class the change is reflected in the textblock. The problem occurs when I try to load the OrderID from another class.

The Other class:

public class ModifyOrder
{
   private ViewModel boundData;
   public ModifyOrder()
   {
      boundData = new ViewModel();
   }

   public void ChangeOrderID()
   {
      boundData.Order.OrderID = 10;
   }
}

The changes here don't get transfered to the static _Order in the ViewModel class.

This is the viewModel class:

public class ViewModel : INotifyPropertyChanged
{
   private Orders _Order;
   public Orders Order
   {
      get { return _Order; }
      set 
      { 
         if (_Order != value)
         {
            _Order = value;
         }
      }
   }

   public ViewModel()
   {
      Order = new Orders();
   }

   public event PropertyChangedEventHandler PropertyChanged;
   private void OnPropertyChanged([CallerMemberName] string propertyName = null)
   {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
}

I have the ViewModel class loaded into the UI's DataContext and my other bound variables in the ViewModel class work fine but for some reason either the bound textblock isn't accessing the Order.OrderID (Which I don't think is the problem since I can modify the Order.OrderID in the ViewModel class and the changes are reflected) OR the class that's modifying my Order isn't able to modify the OrderID.

I've already tried to load a new Order class with the new OrderID and then try to load the ViewModel's _Order with the ModifyOrder's new Order but that hasn't worked out either.

This is the Orders class:

public class Orders : INotifyPropertyChanged
{
   private int _OrderID;
   public int OrderID 
   { 
      get { return _OrderID; } 
      set 
      {
         if(_OrderID != value)
         {
            _OrderID = Value
            OnPropertyChanged(nameof(OrderID));
         }
      } 
   }

   public event PropertyChangedEventHandler PropertyChanged;
   private void OnPropertyChanged([CallerMemberName] string propertyName = null)
   {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
}
<Page x:Class="SPWally.FunctionalPages.LookupOrders"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:SPWally.FunctionalPages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="LookupOrders">

    <Grid Background="AliceBlue">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Text="Search For Order" />

        <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,335,0" Text="OrderID: " />
        <TextBox Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Width="300" Margin="0,0,20,0" Text="{Binding Path=OrderIDSearch, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />

        <Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="40" Height="20" Margin="10,0,0,0" Content="Find" Click="Find_Click" />
        <Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="50" Height="20" Margin="70,0,0,0" Content="Refund" Click="Refund_Click" />

        <TextBlock Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,100,0" Text="OrderID:"/>
        <TextBlock Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,100,0" Text="Customer:"/>
        <TextBlock Grid.Row="5" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,100,0" Text="Product:"/>
        <TextBlock Grid.Row="6" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,100,0" Text="Branch:"/>
        <TextBlock Grid.Row="7" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,100,0" Text="Sales Price:"/>
        <TextBlock Grid.Row="8" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,100,0" Text="Quantity:"/>

        <TextBlock Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Margin="30,0,0,0" Text="{Binding Path=Order.OrderID, Mode=TwoWay}"/>
        <TextBlock Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Margin="30,0,0,0" Text="{Binding Path=Order.Customer.FullName, Mode=TwoWay}"/>
        <TextBlock Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Margin="30,0,0,0" Text="{Binding Path=Order.Product.ProductName, Mode=TwoWay}"/>
        <TextBlock Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Margin="30,0,0,0" Text="{Binding Path=Order.Branch.BranchName, Mode=TwoWay}"/>
        <TextBlock Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Margin="30,0,0,0" Text="{Binding Path=Order.SalesPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Grid.Row="8" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Margin="30,0,0,0" Text="{Binding Path=Order.Stocks, Mode=TwoWay}"/>

        <Button Grid.Row="10" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="60" Height="25" Content="Cancel" Command="{x:Static NavigationCommands.BrowseBack}" />
    </Grid>
</Page>

Keep in mind that I've literally learned everything I know about data binding in the last 48 hours so bear with me here.

Any kind of help at all is very much appreciated. Thank!

You need to implement INotifyPropertyChanged in Orders class. You need to Invoke OnPropertyChanged() inside all setters.

Generated binding classes, This page shows how to create and customize generated binding classes. The generated binding class links the layout variables with the views within the layout . All generated binding classes inherit from the ViewDataBinding class. without data binding, but there are still some instances where access� Sorry for such a delayed response. i could achieve it without using a ViewModel. May not be the best practice, but i could make it work. And i missed to convert the var set to list or Ienumerable i guess. and that prevented it from working But finally without use of ViewModel i could do it. using ViewModel was making the model class redundant.

Where your view model is getting instantiated ? I guess since you are recreating your viewmodel instance in ModifyOrder class boundData = new ViewModel(); binding is lost and may be because of that its not showing up .

A Better Way to Implement Data Binding in .NET, NET Framework since the beginning, data binding became more prominent the View and ViewModel in the Model-View-ViewModel (MVVM) pattern. public int UnreadItemCount { get { return m_unreadItemCount; } set One way to simplify the situation is with a base class, in order to reuse some of the boilerplate logic. If we have many places we have to use this scenario we have the feature in MVC called Custom Model Binding which makes it easier. Let us see this in a simple example. We have UI fields and ViewModel class which has different mappings but we need to map it using custom model binding and save it into database using entity framework.

...Yeah, so. It was the

public ViewModel()
{
   Order = new Order();
}

that was wiping clean the private static Orders _Order in the ViewModel class every time it was instantiated so I changed it to

public ViewModel()
{
   if (_Order == null)
   {
      Order = new Order();
   }
}

and that fixed the problem...

Thanks to everyone that helped me out with this! I learned a lot from you all!

I'm going to go cry myself to sleep now <3

Applying Data Binding for Views, To get started with data binding, we need to make sure to upgrade to the latest View IDs within a layout become variables inside the binding object (i.e binding. You then need to annotate a static method that maps the custom attribute: In this approach, the binding framework connects with the ViewModel, which� ViewModel First. ViewModel first is another method to wire the framework together. In this scenario, the viewmodel is responsible for creating the view and binding itself to the view. You can see an example of this in Rob Eisenberg’s convention-based framework he discussed at MIX: Build your own MVVM Framework.

Android Data Binding and Functional MVVM, Data binding in Android (and many platforms as well) allow you to add objects in your XML Just wrap each field with ObservableField and call the get() or set() method to unwrap. We now set the state variable equal to a CalculatorViewState object. In MVVM, the ViewModel “models” the entire view. Is there any way to access the FirstViewModel's ListViewProperty values and update a ListviewItem in the UpdateViewModel(chid ViewModel) without declaring property as a static. Moreover,I have checkbox on the Firstviewmodel which indicates which type of alerts should be dispayed in the istView.I have to know whether the checkbox is checked or

Creating custom bindings, You're not limited to using the built-in bindings like click , value , and so on — you init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { allBindings — A JavaScript object that you can use to access all the model� Put a property on your AgendaItem called 'Parent', and make it the type of your ViewModel. When new AgendaItem(s) are added to your collection, set their parent to 'this' (your ViewModel) and in your databinding, use "Parent.Clicked" as your binding and see if that works for you. Cheers, Joe

Observable, Generally, almost every UI control could be bound to a data object (all NativeScript controls are created with data binding in mind). After the code has met the� If you have more than one view model, you could the DataContext to the class object where the view model properties are defined, i.e. the window class itself in this case:

Comments
  • You're not invoking OnPropertyChanged in your Order setter. You also need to do it for the properties in your Orders class such as OrderID etc.
  • @MarkFeldman How do you mean invoke OnPropertyChanged for the OrderID? It's in a different class so I can't invoke it there. Do I have to make a copy of the properties in the ViewModel class too in order to do this?
  • Correct. Change notification does not happen "auto-magically", WPF controls specifically look for INotifyPropertyChanged events to update themselves. This means you either have to add INPC to your underlying data objects (generally frowned upon, but can be done either manually or automatically at compile time) or you have to design it into your view model layer and handle updates between that layer and your model layer using whatever method is most convenient for you.
  • I've just changed it right now both in my code and in the question but the problem is still there. Does the PropertyChanged Event handler have to be connected to the one in the ViewModel class too? If so how would I go about that?
  • Can you post the XAML which uses the VM ? "PropertyChanged Event handler have to be connected to the one in the ViewModel class too?" No
  • Okay, I've added it to the question.
  • Is the ViewModel object set as datacontext of the view ? What is the purpose of ModifyOrder class ? You can do those things in the viewmodel
  • Yes I Set the ViewModel to the view's datacontext and the ModifyOrder class is the intermediary class that holds the mysql commands to lead the order and other classes
  • That might be it but how would I go about fixing that?