I Have Two ComboBoxes for example Country and State where State ComboBox's ItemSource depends on Selected Country of First ComboBox.
Country and State Properties are defined in separate CountryModel(CountryID,CountryName) and StateModel(CountryID,StateID,StateName).
Now there is the Model "UserModel" is like :
public class UserModel:ViewModelBase
{
private string _userName;
public string UserName
{
get { return _userName; }
set { _userName = value; OnPropertyChanged("UserName"); }
}
private long _stateID;
public long StateID
{
get { return _stateID; }
set { _stateID = value; OnPropertyChanged("StateID"); }
}
private int _countryID;
public int CountryID
{
get { return _countryID; }
set { _countryID = value; OnPropertyChanged("CountryID"); }
}
}
It has a Service "UserModelService" for populating :
public class UserModelService
{
public UserModelService()
{
}
public ObservableCollection<UserModel> GetUserList()
{...}
public ObservableCollection<CountryModel> GetCountryList()
{...}
public ObservableCollection<StateModel> GetStateList()
{...}
}
Now The UserViewModel is Like:
public class UserViewModel:ViewModelBase,IPageViewModel
{
UserModelService modelService;
public UserViewModel()
{
modelService = new UserModelService();
GetData();
}
private void GetData()
{
UserList = modelService.GetUserList();
CountryList = modelService.GetCountryList();
StateList = modelService.GetStateList();
}
private UserModel _currentUser=new UserModel();
public UserModel CurrentUser
{
get { return _currentUser; }
set
{
if (value == _currentUser) return;
_currentUser = value; OnPropertyChanged("CurrentUser");
}
}
private ObservableCollection<UserModel> _userList;
public ObservableCollection<UserModel> UserList
{
get { return _userList; }
set
{
if (value == _userList) return;
_userList = value;
OnPropertyChanged("UserList");
}
}
private ObservableCollection<CountryModel> _countryList;
public ObservableCollection<CountryModel> CountryList
{
get { return _countryList; }
set { _countryList = value; OnPropertyChanged("CountryList"); }
}
private ObservableCollection<StateModel> _stateList;
public ObservableCollection<StateModel> StateList
{
get { return _stateList; }
set
{
_stateList = value;
OnPropertyChanged("StateList");
}
}
}
Now the Country and State ComboBox's ItemSource is binded to UserViewModels "CountryList" and "StateList" respectively. In this scenario how to I repopulate StateList OnPropertyChange of CurrentUser.CountryID?
EDIT: I Solved it by introducing separate property for Selected Country and OnPropertyChanged CurrentUser.CountryID=selectedCountry. Thanks
Handle the PropertyChanged event for the current user:
public UserModel CurrentUser
{
get { return _currentUser; }
set
{
if (value == _currentUser)
return;
if (_currentUser != null)
_currentUser.PropertyChanged -= OnCurrentUserPropertyChanged;
_currentUser = value;
if (_currentUser != null)
_currentUser.PropertyChanged += OnCurrentUserPropertyChanged;
OnPropertyChanged("CurrentUser");
}
}
private void OnCurrentUserPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(UserModel.CountryID))
{
UserModel userModel = (UserModel)sender;
var states = ...; //get states by userModel.CountryID
StateList = states;
}
}
Related
i have a WPF desktop app.
I load an ObservableCollection with a list of objects.
I use the ICollectionView object to 'wrap' a filter around this ObservableCollection .
I set the filter and refresh but it does nto work so:
public class DataFilters : ViewModelBase
{
private ICollectionView _UserMappedRolesView { get; set; }
private ObservableCollection<UserMappedRoles> _UserMappedRoles;
public ObservableCollection<UserMappedRoles> UserMappedRoles
{
get
{
_UserMappedRolesView = CollectionViewSource.GetDefaultView(_UserMappedRoles);
_UserMappedRolesView.Filter = UserMappedRolesFilter;
_UserMappedRolesView.Refresh();
return _UserMappedRoles;
}
set
{
_UserMappedRoles = value;
}
}
public void LoadUserMappedRoles()
{
var baseData = InformedWorkerBusinessService.UserMappedRoles.Get();
var modelData =
from data in baseData
select new UserMappedRoles
{
Enabled = 1,
Login = data.Login,
UserMappedRolesRef = data.UserMappedRolesRef,
UserRoleRef = data.UserRoleRef
};
_UserMappedRoles = new ObservableCollection<UserMappedRoles>(modelData);
}
public string Login { get; set; }
private bool UserMappedRolesFilter(object item)
{
UserMappedRoles UserMappedRole = item as UserMappedRoles;
if (UserMappedRole.Login== Login)
{
return true;
}
else
{
return false;
}
}
}
and my test script:
UI.InformedWorkerViewModel.Models.HeartBeat VM = new UI.InformedWorkerViewModel.Models.HeartBeat();
VM.CommonData.DataFilters = new UI.InformedWorkerViewModel.Models.DataFilters();
VM.CommonData.DataFilters.LoadUserMappedRoles();
var data = VM.CommonData.DataFilters.UserMappedRoles;
VM.CommonData.DataFilters.Login = "David";
var filtered = VM.CommonData.DataFilters.UserMappedRoles;
I know my data only contains oUserMappedRoles where the Login name is 'Andy' so, by setting the Login filter name to 'David' I expect to get no records back.
I have set breakpoints everywhere and everyline of code gets 'hit'.
Have I (obviously) implemented this wrong?
Thanks
It is the ICollectionView that gets filtered, not the ObservableCollection.
So you should bind to the ICollectionView property:
<ListBox ItemsSource="{Binding UserMappedRolesView}" DisplayMemberPath="Login" />
...or look for the filtered items in this one:
var filtered = VM.CommonData.DataFilters.UserMappedRolesView;
You also need to refresh the CollectionView whenever you want to re-apply the filter, i.e. whenever your Login property is set to a new value. Something like this:
public class DataFilters : ViewModelBase
{
private ICollectionView _UserMappedRolesView;
public ICollectionView UserMappedRolesView
{
get { return _UserMappedRolesView; }
set { _UserMappedRolesView = value; NotifyPropertyChanged(); }
}
private ObservableCollection<UserMappedRoles> _UserMappedRoles;
public ObservableCollection<UserMappedRoles> UserMappedRoles
{
get
{
return _UserMappedRoles;
}
set
{
_UserMappedRoles = value;
NotifyPropertyChanged();
UserMappedRolesView = CollectionViewSource.GetDefaultView(_UserMappedRoles);
UserMappedRolesView.Filter = UserMappedRolesFilter;
UserMappedRolesView.Refresh();
}
}
public void LoadUserMappedRoles()
{
var baseData = InformedWorkerBusinessService.UserMappedRoles.Get();
var modelData =
from data in baseData
select new UserMappedRoles
{
Enabled = 1,
Login = data.Login,
UserMappedRolesRef = data.UserMappedRolesRef,
UserRoleRef = data.UserRoleRef
};
UserMappedRoles = new ObservableCollection<UserMappedRoles>(modelData);
}
private string _login;
public string Login
{
get { return _login; }
set { _login = value; _UserMappedRolesView.Refresh(); }
}
private bool UserMappedRolesFilter(object item)
{
UserMappedRoles UserMappedRole = item as UserMappedRoles;
if (UserMappedRole.Login == Login)
{
return true;
}
else
{
return false;
}
}
}
I have One View which has one Data grid with radio Button , onchecking radio Box , the selected row should go to other View Screen Textbox
here is my first ViewModel
public class CampaignSearchResultsViewModel : ViewModelBase
{
public CampaignSearchResultsViewModel(List<Lead> obj)
{
foreach(Lead lead in obj)
{
SelectedLead = lead;
}
}
public CampaignSearchResultsViewModel()
{
this.Commands.Add("CheckedCommand", new ActionCommand<Lead>(CheckIt));
Commands.Add("OutboundSelect", new ActionCommand<Object>(OutboundSelection));
_leads = new ObservableCollection<Lead>();
}
public ICommand OutboundSelect
{
get
{
return Commands["OutboundSelect"];
}
}
public void OutboundSelection(Object obj)
{
}
private void CheckIt(Lead lead)
{
SelectedLead = lead;
LeadViewModel lmv = new LeadViewModel(this);
}
#region Private
private ObservableCollection<Lead> _leads;
public bool IsChecked { get; set; }
private ICommand _checkedCommand;
private object _testProperty;
private Lead _selectedLead;
private ICollectionView icv;
#endregion
private ICommand _checkedRadioCommand;
private bool _inboundChecked;
#region Properties
public ObservableCollection<Lead> Leads
{
get { return _leads; }
set
{
_leads = value;
FirePropertyChanged("Leads");
}
}
public Lead SelectedLead
{
get { return _selectedLead; }
set { _selectedLead = value; }
}
public ICommand CheckedCommand
{
get
{
return Commands["CheckedCommand"];
}
}
public bool InboundChecked
{
get
{
return _inboundChecked;
}
private set
{
if (_inboundChecked != value)
{
_inboundChecked = value;
FirePropertyChanged("InboundChecked");
}
}
}
#endregion
}
i have to map SelectedLead to the other view model i have pass info to SearchCampaignMembers() method , how
public partial class LeadViewModel : ViewModelBase
{
public void SearchCampaignMembers()
{
_service.Load(_service.SearchCampaignMembersQuery(Entity.FirstName, Entity.LastName), lo =>
{
if (!lo.HasError)
{
ListLead = lo.Entities.ToList();
_savedLeadStatusId = Entity.LeadStatusId;
EntitySet = _service.Leads;
if (ListLead.Count == 1)
{
if (Entity != null)
{
IsVendorLead = Entity.LeadTypeId == Lookups.LeadType.VendorLead;
//Lead Update History
EntityQuery<LeadUpdateHistory> historyquery = null;
historyquery = _service.GetLeadUpdateHistoryByLeadIdQuery(Entity.LeadId);
_service.Load(historyquery, l =>
{
if (!l.HasError)
{
EntityHistory = _service.LeadUpdateHistories;
}
}, null);
//Lead Assignment
EntityQuery<LeadsAssignment> assignmentquery = null;
assignmentquery = _service.GetLeadsAssignmentByLeadIdQuery(Entity.LeadId);
_service.Load(assignmentquery, l =>
{
if (!l.HasError)
{
EntityAssignment = _service.LeadsAssignments;
}
}, null);
if (Entity.LeadTypeId == Lookups.LeadType.PhoneLead)
{
IsInboundLead = Entity.VendorId == null;
IsOutboundLead = Entity.VendorId != null;
}
else
{
IsInboundLead = false;
IsOutboundLead = false;
}
//SelectTimeToCall(Entity);
if (IsOutboundLead)
SelectedCampaign = Entity.LeadCampaigns.FirstOrDefault().Campaign;
else
SelectCampaign(Entity);
OperationsListener listener = new OperationsListener();
listener.Completed += (s, args) =>
{
CompleteInitializing();
//SwitchTab(param.InitialTab);
Action action = () =>
{
SelectDealer(Entity);
};
//GetDealerRecommendation(Entity.Address.ZipCode, action);
SelectStatus(Entity);
//if (callback != null)
// callback();
};
LoadLookupData(listener);
listener.Start();
}
}
else if (ListLead.Count >= 1)
{
CampaignSearchResultsViewModel vm = new CampaignSearchResultsViewModel();
foreach (Lead lead in ListLead)
{
vm.Leads.Add(lead);
ObservableCollection<Lead> abc;
abc = new ObservableCollection<Server.DataAccess.Lead>();
}
ViewController.OpenDialog("SearchCampaignResults", vm, r =>
{
});
}
else if (ListLead.Count == 0)
{
ViewController.OpenDialog("NoResults", (r) =>
{
});
}
}
else
{
//if (callback != null)
// callback();
}
}, null);
}
}
If you use MVVM Light Toolkit, see Messenger class see this answer for sample.
i defined bellow classes for my EF in wpf for use in mvvm pattern:
public class ProductGroup : ViewModelBase
{
public long ID { get; set; }
private string _Title;
public string Title
{
get { return _Title; }
set
{
if (_Title != value)
{
_Title = value;
this.RaisePropertyChanged("Title");
}
}
}
private byte[] _Icon;
public byte[] Icon
{
get { return _Icon; }
set
{
if (_Icon != value)
{
_Icon = value;
this.RaisePropertyChanged("Icon");
}
}
}
private ProductGroup _Parent;
public ProductGroup Parent
{
get { return _Parent; }
set
{
if (_Parent != value)
{
_Parent = value;
this.RaisePropertyChanged("Parent");
}
}
}
private ObservableCollection<ProductGroup> _Childs;
public ObservableCollection<ProductGroup> Childs
{
get { return _Childs; }
set
{
if (_Childs != value)
{
_Childs = value;
this.RaisePropertyChanged("Childs");
}
}
}
private ObservableCollection<Product> _Products;
public ObservableCollection<Product> Products
{
get { return _Products; }
set
{
if (_Products != value)
{
_Products = value;
this.RaisePropertyChanged("Products");
}
}
}
}
public class Product : ViewModelBase
{
public long ID { get; set; }
private ProductGroup _Gorup;
public ProductGroup Gorup
{
get { return _Gorup; }
set
{
if (_Gorup != value)
{
_Gorup = value;
this.RaisePropertyChanged("Gorup");
}
}
}
private string _Name;
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
_Name = value;
this.RaisePropertyChanged("Name");
}
}
}
}
is my definitions correct ??
when i want to remove an item from ProductGroups ,The following error occurs:
An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.
Note:
The target ProductGroup is a child of another ProductGroup
the target ProductGroup has no Child and Product
I'm starting Caliburn Micro development and I have thought of an architecture where a viewmodel has properties, injected by MEF, which are other viewmodels. That way I can use contentcontrols in the view to position them the way I want.
public class ContactsProfileViewModel : Conductor<IContentItem>, IContactsModuleViewModel, IModule, IPartImportsSatisfiedNotification
{
private string name;
private string nameCaption;
private ISingleLineTextContentItem firstName;
private ISingleLineTextContentItem lastName;
public ContactsProfileViewModel()
{
this.DisplayName = "Contact Tab";
}
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
this.NotifyOfPropertyChange(() => Name);
}
}
public string NameCaption
{
get
{
return this.nameCaption;
}
set
{
this.nameCaption = value;
this.NotifyOfPropertyChange(() => NameCaption);
}
}
[Import(typeof(ISingleLineTextContentItem))]
public ISingleLineTextContentItem FirstName
{
get { return this.firstName; }
set
{
this.firstName = value;
this.NotifyOfPropertyChange(() => FirstName);
}
}
[Import(typeof(ISingleLineTextContentItem))]
public ISingleLineTextContentItem LastName
{
get { return this.lastName; }
set
{
this.lastName = value;
this.NotifyOfPropertyChange(() => LastName);
}
}
The viewmodel of SingleLineTextContentItem looks like this:
[Export(typeof(ISingleLineTextContentItem))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class SingleLineTextContentItemViewModel : PropertyChangedBase, ISingleLineTextContentItem
{
private string textBoxText;
private string caption;
public string TextBoxText
{
get { return textBoxText; }
set
{
textBoxText = value;
this.NotifyOfPropertyChange(() => TextBoxText);
}
}
public string Caption
{
get { return caption; }
set
{
this.caption = value;
this.NotifyOfPropertyChange(() => Caption);
}
}
}
Now, I need a way to bind the NameCaption property to the Caption property in a two-way manner. Is that possible? I'm I on the right track with this or is there a better way to do this?
Thanks,
Roland
What I do is instead of having a backing field just route to the other view model
public string NameCaption
{
get
{
return FirstName.Caption;
}
set
{
FirstName.Caption = value;
this.NotifyOfPropertyChange(() => NameCaption);
}
}
However if the Caption property on the ISingleLineTextContentItem can get set independently then you need to register changes on the event and have the view model listen to changes. So instead you need somthing along the lines of:
public string NameCaption
{
get
{
return FirstName == null ? string.Empty : FirstName.Caption;
}
set
{
if(FirstName != null)
FirstName.Caption = value;
}
}
[Import(typeof(ISingleLineTextContentItem))]
public ISingleLineTextContentItem FirstName
{
get { return this.firstName; }
set
{
if(this.FirstName != null)
this.FirstName.PropertyChanged -= FirstNameChanged;
this.firstName = value;
if(this.FirstName != null)
this.FirstName.PropertyChanged += FirstNameChanged;
this.NotifyOfPropertyChange(() => FirstName);
this.NotifyOfPropertyChange(() => NameCaption);
}
}
private void FirstNameChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertName == "Caption")
this.NotifyOfPropertyChange(() => NameCaption);
}
Since either the Caption property or the FirstName property can change then we need to raise the event in the FirstName property and in the handler.
I want to implement functionality that give possibility switch tabItems (TabItem1,TabItem2,TabItem3), when user click button "next" on the Form. And for that reason I'd like to use State Machine Pattern.
These things I've already implemented, but I have no clue, is it right or not?:
public abstract class State
{
#region Constructros
public State()
{
}
public State(State state)
{
this.CurrentState = state.CurrentState;
this.PreviousState = state.PreviousState;
this.NextState = state.NextState;
}
public State(Machine machine,string strCurrentState,string strPreviousState,string strNextState)
{
this.CurrentState = strCurrentState;
this.PreviousState = strPreviousState;
this.NextState = strNextState;
this.SetParams();
}
#endregion
public const string WELCOME = "WELCOME";
public const string EMR_CONFIGURATION = "EMR_CONFIGURATION";
public const string MIGRATION = "MIGRATION";
public const string END_OF_STATES = "END_OF_STATES";
private Machine machine;
private string currentState;
private string previousState;
private string nextState;
private string nameState;
public virtual void SetParams() { }
public virtual void ChangeState(Machine m, State s)
{
m.ChangeState(s);
}
//Get The name of State
public Machine Machine
{
get { return this.machine; }
set { this.machine = value; }
}
//Current State
public string CurrentState
{
get { return this.currentState; }
set { this.currentState = value; }
}
//Previous State
public string PreviousState
{
get { return this.previousState; }
set { this.previousState = value; }
}
//Next State
public string NextState
{
get { return this.nextState; }
set { this.nextState = value; }
}
public string NameState
{
get { return this.nameState; }
set { this.nameState = value; }
}
}
public class Machine
{
public State currentState;
public Machine (string strCurrentState,string strPreviousState,string strNextState)
{
currentState = new WelcomeState(this, strCurrentState, strPreviousState, strNextState);
}
public void ChangeState(State setState)
{
currentState = setState;
}
public void SetCurrentState (string state)
{
currentState.CurrentState = state;
}
}
class Transition
{
public State state;
private static Transition getInstance;
protected Transition(){}
public static Transition GetInstance()
{
if (getInstance == null)
{
getInstance = new Transition();
}
return getInstance;
}
public void Transform(State state)
{
if (state == null)
{
return;
}
// Get the type of state.
string stateType = state.GetType().Name;
// WELCOME TabItem
if (state.CurrentState == State.WELCOME)
{
state.ChangeState(state.Machine, new WelcomeState(state));
}
//EMR_CONFIGURATION TabItem
if (state.CurrentState == State.EMR_CONFIGURATION)
{
state.ChangeState(state.Machine, new EMRConfigurationState(state));
}
//MIGRATION TabItem
if (state.CurrentState == State.EMR_CONFIGURATION)
{
state.ChangeState(state.Machine, new MigrationState(state));
}
}
}
public class WelcomeState : State
{
public WelcomeState(State state) : base(state)
{
}
public WelcomeState (Machine machine, string strCurrentState,string strPreviousState,string strNextState):
base(machine, strCurrentState, strPreviousState, strNextState)
{
}
public override void SetParams()
{
this.CurrentState = State.WELCOME;
this.PreviousState = State.WELCOME;
this.NextState = State.EMR_CONFIGURATION;
}
}
public class EMRConfigurationState : State
{
public EMRConfigurationState(State state) : base(state)
{
}
public EMRConfigurationState(Machine machine, string strCurrentState, string strPreviousState, string strNextState) :
base(machine, strCurrentState, strPreviousState, strNextState)
{
}
public override void SetParams()
{
this.CurrentState = State.EMR_CONFIGURATION;
this.PreviousState = State.WELCOME;
this.NextState = State.MIGRATION;
}
}
public class MigrationState: State
{
public MigrationState(State state) : base(state)
{
}
public MigrationState(Machine machine, string strCurrentState, string strPreviousState, string strNextState) :
base(machine, strCurrentState, strPreviousState, strNextState)
{
}
public override void SetParams()
{
this.CurrentState = State.MIGRATION;
this.PreviousState = State.EMR_CONFIGURATION;
this.NextState = State.END_OF_STATES;
}
}
public partial class Window1 : Window
{
private WizardPresenter wizPresenterWelcome = null;
public List<WizardPresenter> stateList;
public Window1()
{
InitializeComponent();
stateList = new List<WizardPresenter>
{
new WizardPresenter(StatePresenter.WELCOME,
StatePresenter.WELCOME,
StatePresenter.EMR_CONFIGURATION),
new WizardPresenter(StatePresenter.EMR_CONFIGURATION,
StatePresenter.WELCOME,StatePresenter.MIGRATION),
new WizardPresenter(StatePresenter.MIGRATION,
StatePresenter.EMR_CONFIGURATION,
StatePresenter.WELCOME),
};
tabControl.ItemsSource = stateList;
}
private WizardPresenter GetWizardPresenter(string strState)
{
foreach (WizardPresenter presenter in stateList)
{
if (presenter.currentStatePresenter.CurrentStatePresenter == strState)
{
return presenter;
break;
}
}
return null;
}
private void button_Click(object sender, RoutedEventArgs e)
{
if (this.wizPresenterWelcome == null)
{
try
{
{
WizardPresenter wp = (WizardPresenter)tabControl.SelectedItem;
string nextState = wp.currentStatePresenter.NextStatePresenter;
tabControl.SelectedIndex = stateList.IndexOf(GetWizardPresenter(nextState));
}
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message + "Error");
}
}
}
}
If when you run it works then you've got it right, if when you run it doesn't work then you've got it wrong.
In general:
When you're rephrasing a question, edit the original post and add explanations there.
You can create a context class that holds all the relevant states and the current, previous and next state. Each state can handle each change and decide the next state based on:
Runtime value of Context variables.
The context's previous state.
etc.