Reputation: 11570
It's not clear to me on how to wire-up an entity that reflects the binding context of a ListViewItem.
XAML:
<ListView x:Name="AssignedMaterialsList" Grid.Row="7" Grid.ColumnSpan="2" ItemsSource="{Binding AssignedMaterials}" SelectedItem="{Binding SelectedAssignedMaterial}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding Quantity, StringFormat='({0:F0})'}" />
<Label Grid.Row="0" Grid.Column="1" Text="{Binding Name}" />
<Label Grid.Row="0" Grid.Column="2" Text="{Binding ., Converter={StaticResource MaterialToCostConverter}, StringFormat='{}{0:c}'}}" />
<Label Grid.Row="1" Grid.Column="0" Text="{Binding Description}" />
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
POCO:
public class Material : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
decimal _quantity = 0.0m;
public decimal Quantity
{
get { return _quantity; }
set
{
if (_quantity != value)
{
_quantity = value;
OnPropertyChanged();
}
}
}
void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
How does the PropertyChanged event get initialized? Hence, just having the class implement the INotifyPropertyChanged is not enough. The PropertyChanged event usually get's initialized when coupling the view-model to a view when the view gets instantiated.
In my case though, I need this entity to update when any of its properties changes. However, it's not connected to any binding system.
Upvotes: 0
Views: 631
Reputation: 27360
in general, event is "instantiated" (becomes not null) when sombody attaches to it.
think of following:
var material = new Material();
material.Quality = 1;
when you set Quality property, Material.OnPropertyChanged
is called, but PropertyChanged
event is null because there are no eventhandler attached yet.
material.PropertyChanged += Material_PropertyChanged;
material.Quality = 2;
Material.OnPropertyChanged is called again, but this time PropertyChanged is not null.
in xaml, when you apply binding, the framework (either wpf or xamarin or silverlight) attaches to PropertyChanged event it's own eventhandler, that updates the FrameworkElement's property. The framework is also responsible for detaching from the event to avoid memory leaks.
So your responsibility is only to trigger the event and to write bindings.
Upvotes: 1
Reputation: 89214
The consumer of your class is responsible for determining if it wants to subscribe to the PropertyChanged event - this is generally true of any event driven system - a consumer is free to to subscribe to (by providing a handler) or ignore an event as it chooses.
When you use a class that implements INotifyPropertyChanged with a binding system (like Xamarin Forms) the binding mechanism automatically sets up an event handler for the PropertyChanged events.
Upvotes: 1
Reputation: 1543
We'd need to see you XAML to understand - typically you'd have {Binding Quantity} in XAML and somewhere in your code, you'd have DataContext = in a view model.
Upvotes: 0