作者sunny10463 ( )
看板C_Sharp
标题Re: [问题] Android Xamarin DataGrid update
时间Tue Jun 22 20:19:59 2021
有将DataGrid ItemSource属性绑在ObservableCollection
但是还是不会动,请问是否有写错,程式码如下:
view model :
public class ShipVM : ViewModelBase
{
private ObservableCollection<ShipModel> shipList;
public ObservableCollection<ShipModel> ShipList
{
get => shipList;
set
{
shipList = value;
OnPropertyChanged("B");
}
}
}
xaml.cs :
<dg:DataGrid x:Name="grid" ItemsSource="{Binding ShipList>
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="A" PropertyName="A" Width="1*"/>
<dg:DataGridColumn Title="B" PropertyName="B" Width="1*"/>
<dg:DataGridColumn Title="C" PropertyName="C" Width="1*" >
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Entry Text="{Binding .}" Keyboard="Numeric" />
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
<dg:DataGridColumn Title="D" PropertyName="D" Width="1*">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Entry Text="{Binding .}" Keyboard="Numeric" />
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
<dg:DataGridColumn Title="E" PropertyName="E" Width="1*"/>
</dg:DataGrid.Columns>
</dg:DataGrid>
※ 引述《e23882 (呆呆要不要买降有)》之铭言:
: 让DataGrid Itemsource属性绑定在ObservableCollection资料集合上
: public ObservableCollection<YourDataModel> Collection
: {
: get{....}set{....}
: }
: 这样在Collection属性新增或是删除的时候元件会接收到通知更新画面
: 但是修改的时候不会更新画面
: 所以你要让YourDataModel继承INotifyPropertyChanged介面,并实作方法
: 让DataModel之间属性变更时通知其他属性
: public class YourDataModel:INotifyPropertyChanged
: {
: private string _PropertyA = string.empty;
: public string PropertyA
: {
: set
: {
: if(_Property != value)
: {
: _PropertyA = value;
: OnPropertyChanged("A");
: OnPropertyChanged("E");
: }
: }
: }
: }
: ※ 引述《sunny10463 ( )》之铭言:
: : 最近在写xamarin遇到个问题
: : 图一:https://imgur.com/4vILlbA.jpg
: : 图二:https://imgur.com/PIErXTB.jpg
: : E = (B-C)*D
: : 图一为一开始以MVVM架构载入,会自动计算栏位E
: : 但是图二,修改栏位D(或C)栏位时,不会自动计算栏位E
: : 请问各位高手该用何事件?
: : 程式码:
: : <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
: : xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
: : xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid">
: : <dg:DataGrid.Columns>
: : <dg:DataGridColumn Title="A" PropertyName="A" Width="1*"/>
: : <dg:DataGridColumn Title="B" PropertyName="B" Width="1*"/>
: : <dg:DataGridColumn Title="C" PropertyName="C" Width="1*" >
: : <dg:DataGridColumn.CellTemplate>
: : <DataTemplate>
: : <Entry Text="{Binding .}" Keyboard="Numeric" />
: : </DataTemplate>
: : </dg:DataGridColumn.CellTemplate>
: : </dg:DataGridColumn>
: : <dg:DataGridColumn Title="D" PropertyName="D" Width="1*">
: : <dg:DataGridColumn.CellTemplate>
: : <DataTemplate>
: : <Entry Text="{Binding .}" Keyboard="Numeric"
: : Completed="Entry_Completed"/>
: : </DataTemplate>
: : </dg:DataGridColumn.CellTemplate>
: : </dg:DataGridColumn>
: : <dg:DataGridColumn Title="E" PropertyName="E"
: : BindingContext="{Binding E}" Width="1*"/>
: : </dg:DataGrid.Columns>
: : </ContentPage>
: : 谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 180.217.73.9 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1624364401.A.9CA.html
1F:→ henry78925: 他说的是你的ShipModel 不是ShipVM 07/11 04:21