I'm new to WPF + MVVM and have been having trouble getting around viewmodels.
I have a object called FSystem which contains a alot of lists which are populated from a XML.
public class FSystem : ObservableObject
{
public List<FUser> _userList;
public List<FZone> _zoneList;
public List<FSource> _sourceList;
public string _projectName { get; set; }
private string _projectVersion { get; set; }
private string _processorIp { get; set; }
private bool _isMultiLingualModeOn { get; set; }
private int _systemIncludeLighting { get; set; }
private int _systemIncludeWindowsTreatments { get; set; }
private int _systemIncludeSip { get; set; }
private int _systemIncludeCamaras { get; set; }
public FSystem()
{
UserList = new List<FUser>();
}
}
This is the XMLParser which is called when the user loads the XML to the application.
public static class XMLParsers
{
public static FSystem ParseByXDocument(string xmlPath)
{
var fSystem = new FSystem();
XDocument doc = XDocument.Load(xmlPath);
XElement fSystemElement = doc.Element("FSystem");
if (fSystemElement != null)
{
fSystem.ProjectName = fSystemElement.Element("ProjectName").Value;
fSystem.ProjectVersion = fSystemElement.Element("ProjectVersion").Value;
fSystem.ProcessorIp = fSystemElement.Element("ProcessorIP").Value;
fSystem.ProcessorFilePath = fSystemElement.Element("ProcessorFilePath").Value;
fSystem.SystemIncludeLighting = Convert.ToInt16(fSystemElement.Element("SystemIncludeLighting").Value);
fSystem.SystemIncludeSip = Convert.ToInt16(fSystemElement.Element("SystemIncludeLighting").Value);
fSystem.SystemIncludeCamaras = Convert.ToInt16(fSystemElement.Element("SystemIncludeCameras").Value);
}
fSystem.UserList = (from user in doc.Descendants("FUser")
select new FUser()
{
Id = user.Element("Id").Value,
Name = user.Element("Name").Value,
Icon = user.Element("IconColour").Value,
Pin = user.Element("UserPin").Value,
IsPinEnabled = Convert.ToBoolean(Convert.ToInt16(user.Element("UserPinEnabled").Value)),
ListIndex = user.Element("ListIndex").Value
}).ToList();
return fSystem;
}
}
And this is the MainViewModel below is what contains the Commands which Load the XML and the property FSystem I wish to use in other view models.
public class MainViewModel : ViewModel
{
private Fystem fSystem;
public FSystem FSystem
{
get { return fSystem; }
private set
{
fSystem = value;
NotifyPropertyChanged("FSystem");
}
}
public MainViewModel()
{
InitiateState();
WireCommands();
}
private void InitiateState()
{
FSystem = new FSystem();
}
private void WireCommands()
{
XDocumentLoadCommand = new RelayCommand(XDocumentLoad) {IsEnabled = true};
ClearDataCommand = new RelayCommand(ClearData) {IsEnabled = true};
}
public RelayCommand XDocumentLoadCommand { get; private set; }
private void XDocumentLoad()
{
var openDlg = new OpenFileDialog
{
Title = "Open .FAS",
DefaultExt = ".fas",
Filter = "F System Files (*.fas)|*.fas",
Multiselect = false
};
bool? result = openDlg.ShowDialog() == DialogResult.OK;
if (result != true) return;
FSystem = XMLParsers.ParseByXDocument(openDlg.FileName);
}
The application basically lets the user change the different objects (FUser,FZone,FSource, ect). The idea I had was the user would load the XML then be able to edit the different list objects on different views.
What would the correct way be to go about this in MVVM?
I plan to (hopefully) get the User, Zone and Source views to display Datagrids which are populated with their respective data from the Model.
Create you specific view models, and use dependency injection to pass the relevant data into them (this list or that list).
This way, the view models don't need to know about other stuff, and you can easily mock it for testing and for dummy data to see on the designer.
Copy paste into Linqpad for the simplest example. Both mock viewmodels take a dependency (i in our case). You can just pass your lists:
void Main()
{
int someInt = 5;
int anotherInt = 7;
VM vm1 = new VM(someInt);
VM vm2 = new VM(anotherInt);
vm1.RevealI();
vm2.RevealI();
}
public class VM{
private int _i;
public VM(int i)
{
_i = i;
}
public void RevealI() { Console.WriteLine("value of i is: " + _i); }
}
Othen than that, here's more items:
MSDN
Code Project
stack overflow
Related
I have class
public class Gallery
{
public string method { get; set; }
public List<List<object>> gidlist { get; set; }
public int #namespace { get; set; }
}
Button code
private void button1_Click(object sender, EventArgs e)
{
List<object> data = new List<object>();
data.Add(618395);
data.Add("0439fa3666");
Gallery jak = new Gallery();
jak.method = "gdata";
jak.gidlist.Add(data);
jak.#namespace = 1;
string json = JsonConvert.SerializeObject(jak);
textBox2.Text = json;
}
Here I get System.NullReferenceException. How to add item to gidlist ?
You get it because in now place you initialized the list within jak.
You can:
Add a default constructor and initialize list there:
public class Gallery
{
public Gallery()
{
gidlist = new List<List<object>>();
}
public string method { get; set; }
public List<List<object>> gidlist { get; set; }
public int #namespace { get; set; }
}
If in C# 6.0 then you can use the auto-property initializer:
public List<List<object>> gidlist { get; set; } = new List<List<object>>()
If in under C# 6.0 and don't want the constructor option for some
reason:
private List<List<object>> _gidlist = new List<List<object>>();
public List<List<object>> gidlist
{
get { return _gidlist; }
set { _gidlist = value; }
}
You can just initialize it before using (I don't recommend this option)
Gallery jak = new Gallery();
jak.method = "gdata";
jak.gidlist = new List<List<object>>();
jak.gidlist.Add(data);
jak.#namespace = 1;
If before C# 6.0 best practice will be option 1. If 6.0 or higher then option 2.
I'm trying to update another ViewModel when a button is clicked. That Viewmodel already has a button that updates its own ViewModel but I want the exact same functionallity on another viewModel
Here is my code:
OlyckorViewModel (The view that I want to update and that already has a button that updates it) SearchActiveInvestigationsCommand is the button binding that updates it
public class OlyckorViewModel : NotificationObject
{
private readonly ISosServiceDelegate _sosService;
private readonly IDialogService _dialogService;
private string _searchConditionDiarienummer;
private string _searchConditionFartygsnamn;
private string _searchConditionRegisterbeteckning;
private int? _searchConditionIMONummer;
private DateTime? _searchConditionHaendelseDatum;
private string _searchConditionOlyckshaendelsetypKod;
private bool _isSearching;
private bool _isSearchExpanded = true;
private ObservableCollection<SosListOlycka> _olyckor;
private SosListOlycka _selectedOlycka;
private string _statusText;
public OlyckorViewModel(ISosServiceDelegate sosService, ILoggerFacade logger, IDialogService dialogServce)
{
logger.Log("Initializing OlyckorViewModel", Category.Debug, Priority.Low);
_sosService = sosService;
_dialogService = dialogServce;
SearchCommand = new DelegateCommand(ExecuteSearch, CanExecuteSearch);
SearchActiveInvestigationsCommand = new DelegateCommand(ExecuteSearchActiveInvestigations, CanExecuteSearchActiveInvestigations);
OpenCommand = new DelegateCommand(ExecuteOpen, CanExecuteOpen);
DeleteCommand = new DelegateCommand(ExecuteDelete, CanExecuteDelete);
CreatePdfReportCommand = new DelegateCommand(ExecuteCreatePdfReport, CanExecuteCreatePdfReport);
}
private void ExecuteSearch()
{
if (IsSearching)
return;
IsSearching = true;
StatusText = "Söker olyckor...";
var criteria = new SosOlyckaSearchCriteria();
criteria.Diarienummer = SearchConditionDiarienummer;
criteria.Fartygsnamn = SearchConditionFartygsnamn;
criteria.Registerbeteckning = SearchConditionRegisterbeteckning;
criteria.IMONummer = SearchConditionIMONummer;
criteria.HaendelseDatum = SearchConditionHaendelseDatum;
criteria.OlyckshaendelseTypKod = SearchConditionOlyckshaendelsetypKod;
_sosService.SearchOlyckor(
criteria,
olyckor =>
{
HandleResultFromSearch(olyckor);
},
exception =>
{
IsSearching = false;
StatusText = "Misslyckades att söka olyckor";
DialogHelper.ShowException(exception);
}
);
}
private void HandleResultFromSearch(SosSearchResult<SosListOlycka> olyckor)
{
IsSearching = false;
IsSearchExpanded = false;
Olyckor = new ObservableCollection<SosListOlycka>(olyckor.Items);
StatusText = Olyckor.Count + " " + (Olyckor.Count == 1 ? "olycka." : "olyckor.");
if (olyckor.IsResultTruncated)
{
var statusTextResultTruncated = "Resultatet var för stort (" + olyckor.OriginalNumberOfHits + " olyckor) och trunkerades på servern.";
StatusText += " " + statusTextResultTruncated;
}
// if search result only contains a single item, it should be opened
if (Olyckor.Count == 1)
DialogHelper.OpenOlyckaDialog(Olyckor[0].OlyckaId);
}
OlyckaViewModel (The viewModel that is supposed to have a save button to update the viewmodel above) SaveCommand is the button binding that I want to update like the button above
public class OlyckaViewModel : DialogWindowViewModel
{
private readonly ISosServiceDelegate _sosService;
private readonly Repository _repository;
private readonly IDialogService _dialogService;
private readonly IInteractionService _interactionService;
private readonly FroCodesViewModel _froCodesViewModel;
private SosOlycka _model;
private SosOlycksorsak _selectedOlycksorsak;
private SosOlycksorsak _selectedHuvudorsak;
private SosStegIOlycksfoerlopp _selectedStegIOlycksfoerlopp;
private SosStegIOlycksfoerlopp _selectedInledandeSteg;
private SosOlycka _selectedOtherOlycka;
private OlyckorViewModel _olyckorlist;
private readonly ObservableCollection<CheckBoxListItemViewModel<TrsFartygsunderkategori>> _fartygsunderkategorier = new ObservableCollection<CheckBoxListItemViewModel<TrsFartygsunderkategori>>();
private int? _olycksrapportIdToHaemtmarkeraWhenSaving;
private ObservableCollection<SosListOlycka> _olyckor;
private SosListOlycka _selectedOlycka;
private bool _isSearching;
private bool _isSearchExpanded = true;
private readonly List<KnownValue> _oestEllerVaest = new List<KnownValue>()
{
new KnownValue("Välj", null),
new KnownValue("E", "E"),
new KnownValue("W", "W")
};
private readonly List<KnownValue> _nordEllerSyd = new List<KnownValue>()
{
new KnownValue("Välj", null),
new KnownValue("N", "N"),
new KnownValue("S", "S")
};
public OlyckaViewModel(ISosServiceDelegate sosService, Repository repository, IDialogService dialogService, IInteractionService interactionService)
{
_sosService = sosService;
_repository = repository;
_dialogService = dialogService;
_interactionService = interactionService;
_froCodesViewModel = new FroCodesViewModel(ServiceLocator.Current.GetInstance<ISitsServiceDelegate>());
SaveCommand = new DelegateCommand(ExecuteSave);
ReloadCommand = new DelegateCommand(ExecuteReload, CanExecuteIfExistingOlycka);
CloseCommand = new DelegateCommand(Close);
DeleteCommand = new DelegateCommand(ExecuteDelete, CanExecuteIfExistingOlycka);
AvslutaUtredningCommand = new DelegateCommand(ExecuteAvslutaUtredning, CanExecuteIfExistingOlycka);
CreatePdfReportCommand = new DelegateCommand(ExecuteCreatePdfReport, CanExecuteIfExistingOlycka);
AddOlycksorsakCommand = new DelegateCommand(ExecuteAddOlycksorsak);
RemoveOlycksorsakCommand = new DelegateCommand(ExecuteRemoveOlycksorsak, CanExecuteRemoveOlycksorsak);
AddStegIOlycksfoerloppCommand = new DelegateCommand(ExecuteAddStegIOlycksfoerlopp);
RemoveStegIOlycksfoerloppCommand = new DelegateCommand(ExecuteRemoveStegIOlycksfoerlopp, CanExecuteRemoveStegIOlycksfoerlopp);
FetchFartygsinformationFromSitsCommand = new DelegateCommand(ExecuteFetchFartygsinformationFromSits);
NewOlyckaFromHaendelseCommand = new DelegateCommand(ExecuteNewOlyckaFromHaendelse);
OpenOtherOlyckaCommand = new DelegateCommand(ExecuteOpenOtherOlycka, CanExecuteOpenOtherOlycka);
MoveOlyckaToNewHaendelseCommand = new DelegateCommand(ExecuteMoveOlyckaToNewHaendelse);
MoveOlyckaToOtherHaendelseCommand = new DelegateCommand(ExecuteMoveOlyckaToOtherHaendelse);
MarkSelectedOlycksorsakAsHuvudorsakCommand = new DelegateCommand(ExecuteMarkSelectedOlycksorsakAsHuvudorsak);
MarkSelectedStegIOlycksfoerloppAsInledandeStegCommand = new DelegateCommand(MarkSelectedStegIOlycksfoerloppAsInledandeSteg);
SearchActiveInvestigationsCommand = new DelegateCommand(ExecuteSearchActiveInvestigations, CanExecuteSearchActiveInvestigations);
}
public DelegateCommand SaveCommand { get; private set; }
public DelegateCommand ReloadCommand { get; private set; }
public DelegateCommand CloseCommand { get; private set; }
public DelegateCommand DeleteCommand { get; private set; }
public DelegateCommand AvslutaUtredningCommand { get; private set; }
public DelegateCommand CreatePdfReportCommand { get; private set; }
public DelegateCommand AddOlycksorsakCommand { get; private set; }
public DelegateCommand RemoveOlycksorsakCommand { get; private set; }
public DelegateCommand AddStegIOlycksfoerloppCommand { get; private set; }
public DelegateCommand RemoveStegIOlycksfoerloppCommand { get; private set; }
public DelegateCommand FetchFartygsinformationFromSitsCommand { get; private set; }
public DelegateCommand NewOlyckaFromHaendelseCommand { get; private set; }
public DelegateCommand OpenOtherOlyckaCommand { get; private set; }
public DelegateCommand MoveOlyckaToNewHaendelseCommand { get; private set; }
public DelegateCommand MoveOlyckaToOtherHaendelseCommand { get; private set; }
public DelegateCommand MarkSelectedOlycksorsakAsHuvudorsakCommand { get; private set; }
public DelegateCommand MarkSelectedStegIOlycksfoerloppAsInledandeStegCommand { get; private set; }
public DelegateCommand SearchActiveInvestigationsCommand { get; private set; }
public DelegateCommand SearchCommand { get; private set; }
public FroCodesViewModel FroCodesViewModel { get { return _froCodesViewModel; } }
public OlyckorViewModel OlyckorViewModel { get { return _olyckorlist; } }
public SosOlycka Model
{
get { return _model; }
private set
{
if (value != _model)
{
_model = value;
RaisePropertyChanged(() => Model);
RaisePropertyChanged(() => IsExistingOlycka);
RaiseCanExecuteChangedForOlyckaCommands();
LoadDescriptionsForFroCodes();
SynchronizeFromModel();
}
}
}
private void ExecuteSave()
{
if (IsBusy)
throw new InvalidOperationException("Cannot save olycka, is already busy");
SynchronizeToModel();
// Validate ad-acta
List<string> missingAdActaFields = null;
if (Model.MyndighetensUtredningAvslutad.HasValue)
{
missingAdActaFields = DataHelper.ValidateAdActa(Model);
}
// Validate skrovskada
List<string> missingSkrovskadaFields = null;
if (Model.SosFartygsskada.Skrovskada == "J")
{
missingSkrovskadaFields = DataHelper.ValidateSkrovskada(Model);
}
if ((missingAdActaFields != null && missingAdActaFields.Count > 0)
|| (missingSkrovskadaFields != null && missingSkrovskadaFields.Count > 0))
{
// Bring up window of missing fields (or update if already open)
var vm = _dialogService.GetOpenDialogs().OfType<MissingFieldsViewModel>().FirstOrDefault();
if (vm != null)
{
vm.AdActaFields = missingAdActaFields;
vm.SkrovskadaFields = missingSkrovskadaFields;
_dialogService.Activate(vm);
}
else
{
vm = ServiceLocator.Current.GetInstance<MissingFieldsViewModel>();
vm.AdActaFields = missingAdActaFields;
vm.SkrovskadaFields = missingSkrovskadaFields;
_dialogService.Show(vm);
}
//
// Cancel save operation
return;
}
else
{
// Close window with missing fields if open)
var vm = _dialogService.GetOpenDialogs().OfType<MissingFieldsViewModel>().FirstOrDefault();
if (vm != null)
{
vm.Close();
}
}
SetBusy("Sparar ändringar...");
_sosService.SaveOlycka(
Model,
result =>
{
// load olycka using the id returned from save method (useful if olycka was new / not previously persisted)
ResetBusy();
if (IsGoingToHaemtmarkeraWhenSaving)
{
SetBusy("Markerar olycksrapport som hämtad...");
var rosService = ServiceLocator.Current.GetInstance<IRosServiceDelegate>();
rosService.HaemtmarkeraOlycksrapport(
OlycksrapportIdToHaemtmarkeraWhenSaving.Value,
result,
() =>
{
ResetBusy();
OlycksrapportIdToHaemtmarkeraWhenSaving = null;
LoadOlycka(result);
},
haemtmarkeraException =>
{
ResetBusy();
DialogHelper.ShowException(haemtmarkeraException);
}
);
}
else
{
LoadOlycka(result);
}
},
exception =>
{
ResetBusy();
DialogHelper.ShowException(exception);
}
);
ExecuteSearchActiveInvestigations();
}
I know it's alot of code, I just wanted to make sure not to miss anything.
I tried to copy the same code from the already working button to the button I want to work the same but it didn't work and I suspect it's because I didn't tell it to update another view and not itself. I could be wrong tho.
I appreciate any help,
Raise an event from OlyckaViewModel with the required parameters and subscribe in OlyckorViewModel.
Make the code common in the SearchActiveInvestigationsCommand and call the same function from the subscribed event method.
I'm trying to update another ViewModel when a button is clicked.
In the another viewmodel centralize the update code into a public method.
In the App code, make a static reference to the VM in question.
Make the another viewmodel place a reference to itself onto the app as a static reference.
Centralize the update code into a public method on that VM. The method may have multiple variables which have to be pass through to do any update from any foreign VMs or button clicks from views.
On the button click, get the global app from (see Application.Current Property (System.Windows)) and find the reference to the VM in question. Then call the update method.
I have 2 classes that hold some info about the user. One holds the data, the other one holds the controls info. Now i need to pass both of them as a parameters to a method. However they are connected to one another and i dont think that passing the 2 classes separately as parameters is okay. I wonder if i should put them in a Tuple or something that keeps them together so i can pass them as just 1 parameter to any method. Here's how they look :
The data class :
public class UsersProperties
{
public enum CUser
{
Player,
Bot1,
Bot2,
Bot3,
Bot4,
Bot5
}
public int RightCard { get; set; }
public string Name { get; set; }
public int? Chips { get; set; }
public int Type { get; set; }
public bool Turn { get; set; }
public bool FoldTurn { get; set; }
public int PreviousCall { get; set; }
public int LeftCard { get; set; }
public double Power { get; set; }
public int EnumCasted { get; set; }
}
The controls class :
public class UserControls
{
public Point CardsLocation { get; set; }
public AnchorStyles CardsAnchor { get; set; }
public Panel Panel { get; } = new Panel();
public Point PanelLocation { get; set; }
public Size PanelSize { get; } = new Size((Settings.Width + 10) * 2, Settings.Height + 20);
public int IndentationPanelXy { get; } = 10;
public Label UsernameLabel { get; set; } = new Label();
public Point UsernameLabelLocation { get; set; }
public Size UsernameLabelSize { get; } = new Size(Settings.Width * 2, 20);
public TextBox ChipsTextBox { get; set; } = new TextBox();
public Label StatusLabel { get; set; } = new Label();
public UserControls(AnchorStyles style, Point cardsLocation, bool down)
{
CardsAnchor = style;
CardsLocation = cardsLocation;
UsernameLabelLocation = down ? new Point(CardsLocation.X, CardsLocation.Y - 20) : new Point(CardsLocation.X, CardsLocation.Y + Settings.Height);
PanelLocation = new Point(CardsLocation.X - IndentationPanelXy, CardsLocation.Y - IndentationPanelXy);
}
}
Now here's how i initialize them :
private static Player Player = new Player(Properties.Settings.Default.StartingChips);
private UserControls PlayerControls = new UserControls(AnchorStyles.Bottom, new Point(560, 470),false);
And here's the method where i need to pass both of them :
private void SetPlayers(UsersProperties user, UserControls userControls, int turn, ref bool check, Image refreshbackImage)
{
if (user.Chips <= 0) return;
_foldedPlayers--;
if (turn < user.RightCard || turn > user.LeftCard) return;
if (Holder[user.RightCard].Tag != null)
{
Holder[user.LeftCard].Tag = _reserve[user.LeftCard];
}
Holder[user.RightCard].Tag = _reserve[user.RightCard];
if (!check)
{
_horizontal = userControls.CardsLocation.X;
_vertical = userControls.CardsLocation.Y;
}
check = true;
Holder[turn].Anchor = userControls.CardsAnchor;
Holder[turn].Image = refreshbackImage;
if (turn < Bot1.RightCard)
{
Holder[turn].Image = Deck[_i];
}
Holder[turn].Location = new Point(_horizontal, _vertical);
_horizontal += Holder[turn].Width;
Holder[turn].Visible = true;
Controls.Add(userControls.Panel);
userControls.Panel.Location = userControls.PanelLocation;
userControls.Panel.BackColor = Color.DarkBlue;
userControls.Panel.Size = userControls.PanelSize;
userControls.Panel.Visible = false;
if (_i != user.LeftCard) return;
check = false;
}
I want to know if there's any better way to pass 2 classes as parameters i think they should stick together anyway.
You can write a wrapper class (ie:UserContainer) which has two properties namely, usercontrol and userproperty.
Then initialize it and pass this class as parameter.
I think you could use Generics, as in here:
namespace PropertiesControls
{
public class PropsControls
{
public static void PropesAndControls<TP, TC>(TP property, TC control) where TP : UserProperties where TC: UserControls
{
//your code here
}
}
}
A generic defines a behaviour that can be applied to a class. It is used in collections, where the same approach to handling an object can be applied regardless of the type of object. I.e, a list of strings or a list of integers can be handled using the same logic without having to differentiate between both specific types.
I have a ListView with an List<List<enum>> property and i have a View that shows each bool as button.
I want to cycle through the bound enum when someone clicks on the button. The problem is I cant use a normal click because it would be outside of my ViewModel.
Edit:
I have the class TruthTable that has 2 DynTable:
public sealed class Column<T>
{
public Column()
{
ColumnData = new List<T>();
ColumnHeader = "";
}
...
public List<T> ColumnData { get; set; }
public string ColumnHeader { get; set; }
}
public sealed class DynTable<T>
{
public DynTable()
{
Columns = new List<Column<T>>();
}
...
public List<Column<T>> Columns { get; set; }
}
public sealed class TruthTable
{
public TruthTable()
{
input = new DynTable<bool>();
results = new DynTable<BoolState>();
}
...
private DynTable<bool> input;
private DynTable<BoolState> results;
public DynTable<bool> Input { get { return input; } set { input = value; } }
public DynTable<BoolState> Results { get { return results; }}
}
public enum BoolState
{
False = 0,
True = 1,
DontCare = 2
}
And i have a ViewModel for the TruthTable. I dont think that the I have to show the code for the ViewModel because its just a TruthTable property. I hope thats enough code to understand my problem ._.
i have a issue, I want to Update a value to element which is getting added in a list.
With generic example:-
I have a Model Object:-
public class Model
{
public int ModelProperty1 { get; set; }
public int ModelProperty2 { get; set; }
public int ModelPropertyStatus { get; set; }
}
I have a DTO Object:-
public class DTO
{
public int DTOProperty1 { get; set; }
public int DTOProperty2 { get; set; }
public int DTOPropertyStatus { get; set; }
}
Now, in my Controller i have a List which adds Model object:-
List<Model> _listOfModel = new List<Model>();
Secondly, i have created a mapping method which maps my Model & DTO
private Model MapDTOToModel(DTO dto)
{
return new Model
{
ModelProperty1 = dto.DTOProperty1,
ModelProperty2 = dto.DTOProperty2
};
}
Coming to my Issue:-
I want something like this to work:-
//I want a piece of code that Updates my ModelPropertyStatus after it gets inserted to //List
_listOfModel.Add(new Model() { ModelPropertyStatus = 1 });
//Here is the piece of code i want to convert:-
Model model = new Model();
model.ModelPropertyStatus = 1;
_listOfModel.Add(MapDTOToModel(model));
To be specific(Updated)
I want something like this:-
_listOfModel.Add(MapDTOToModel() { ModelPropertyStatus = 1 });
Any Suggestions??
You are looking for an ObservableCollection:
Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
Here's an example:
public class Model
{
public int ModelProperty1 { get; set; }
public int ModelProperty2 { get; set; }
public int ModelPropertyStatus { get; set; }
}
void Main()
{
ObservableCollection<Model> _listOfModel = new ObservableCollection<Model>();
_listOfModel.CollectionChanged += (s, o) =>
{
foreach (var m in o.NewItems)
((Model)m).ModelPropertyStatus = 1;
};
var model = new Model();
Console.WriteLine("Before add: " + model.ModelPropertyStatus.ToString());
_listOfModel.Add(model);
Console.WriteLine("After add: " + model.ModelPropertyStatus.ToString());
}
output:
Before add: 0
After add: 1
As you can see, using the CollectionChanged event, the property gets updatet during the insert.