我不清楚如何连接反映ListViewItem的绑定上下文的实体。使用POCO实现INotifyPropertyChanged
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));
}
怎样的的PropertyChanged事件得到初始化?因此,仅仅让类实现INotifyPropertyChanged是不够的。当视图被实例化时,将视图模型耦合到视图时,PropertyChanged事件通常会被初始化。
在我的情况下,我需要更新这个实体的任何属性。但是,它没有连接到任何绑定系统。
bleh,这些“表达式”的方法定义很难看:) – Liero
您的视图是直接绑定到您的POCO还是VM?如果它是一个VM,VM和POCO之间的关系是什么?我认为你需要展示更多的代码,以便了解你的绑定是如何设置的。 – Jason
我的视图直接绑定到虚拟机。虚拟机有一个可观察的poco集合,我需要我的视图来反映基于poco中属性值更改的更新。 –