I use Prism MVVM in my app. So here is my XAML code
IsRefreshing="{Binding IsRefreshing}"
Command="{Binding RefreshCommand}">
<CollectionView ItemsSource="{Binding Images}"
RemainingItemsThreshold="1"
RemainingItemsThresholdReachedCommand="{Binding UpdateImagesCommand}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="3"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="2">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ff:CachedImage
Grid.Row="0"
Grid.Column="0"
HorizontalOptions="Center"
VerticalOptions="Center"
Aspect="AspectFill"
Source="{Binding Source, Mode=OneWay}">
<ff:CachedImage.Transformations>
<ffTransformations:CropTransformation />
</ff:CachedImage.Transformations>
</ff:CachedImage>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
As you can see here I have parameter RemainingItemsThreshold with the value "1", and binding anUpdateImagesCommand to the RemainingItemsThresholdReachedCommand
Here is a part of a code in view model of this xaml page
private ObservableCollection _images;
public ObservableCollection Images
{
get { return _images; }
set { SetProperty(ref _images, value); }
}
private DelegateCommand _refreshCommand;
public DelegateCommand RefreshCommand =>
_refreshCommand ?? (_refreshCommand = new DelegateCommand(ExecuteRefreshCommand));
private DelegateCommand _updateImagesCommand;
public DelegateCommand UpdateImagesCommand =>
_updateImagesCommand ?? (_updateImagesCommand = new DelegateCommand(ExecuteUpdateImagesCommand));
void ExecuteUpdateImagesCommand()
{
//Image im = new Image();
//im.Source = "Huayra3.jpg";
//Image im7 = new Image();
//im7.Source = "Huayra1.jpg";
//Images.Add(im7);
//Images.Add(im7);
//Images.Add(im);
//Images.Add(im);
//Images.Add(im);
//Images.Add(im);
}
void ExecuteRefreshCommand()
{
// refresh posts feed command
IsRefreshing = false;
}
public AccountPageViewModel(INavigationService navigationService, IUserDataService userDataService) :
base(navigationService)
{
_userDataService = userDataService;
CurrentUser = userDataService.CurrentUser;
if (String.IsNullOrEmpty(this.CurrentUser.ImagePath))
{
this.CurrentUser.ImagePath = "user_avatar.jpg";
}
Images = new ObservableCollection<Image>();
Image im = new Image();
im.Source = "Huayra3.jpg";
Images.Add(im);
Image im1 = new Image();
im1.Source = "Huayra1.jpg";
Images.Add(im1);
Image im2 = new Image();
im2.Source = "Huayra4.jpg";
Images.Add(im2);
Image im3 = new Image();
im3.Source = "Huayra3.jpg";
Images.Add(im3);
Image im4 = new Image();
im4.Source = "Huayra1.jpg";
Images.Add(im4);
Image im5 = new Image();
im5.Source = "Huayra4.jpg";
Images.Add(im5);
Image im6 = new Image();
im6.Source = "Huayra3.jpg";
Images.Add(im6);
Image im7 = new Image();
im7.Source = "Huayra1.jpg";
Images.Add(im7);
Images.Add(im7);
Images.Add(im7);
Images.Add(im7);
Images.Add(im7);
Images.Add(im7);
Images.Add(im7);
Images.Add(im);
//Images.Add(im);
//Images.Add(im);
}
As you can see I in my VIewModel constructor I've written some values and add it to list. I read microsoft documentation about CollectionView and I know that the UpdateImagesCommand will execute only when the RemainingItemsThreshold is reached. But ExecuteUpdateImagesCommand() invokes when I navigate to this page, when I navigating between my pages in my tabbed page randomly. This should not be, but I do not know the reason why this happens. Can smbd help me please?
I've found another way to fix this problem. I can use the IsActive PRISM property to undertand that this page is that I need.
Related
I have the following code.
The remoteCommand is executing after 5 seconds the method and update the h:form.
<h:form>
<p:remoteCommand name="checkPageRc" autoRun="true"
resetValues="true" async="false"
delay="5000"
actionListener="#{previewPhotoboothProfileController.doLoadCurrentPage(previewPhotoboothProfileController.selectedPhotoboothPage)}" />
</h:form>
<h:form id="previewPageForm">
<o:graphicImage dataURI="true"
rendered="#{previewPhotoboothProfileController.photoboothTemplate.photoboothCurrentSession.singlePictureOutput != null}"
value="#{storageOutputHelperController.loadImageByByteArray(previewPhotoboothProfileController.photoboothTemplate.photoboothCurrentSession.singlePictureOutput)}"
cache="false" />
</h:form>
My backend bean:
#GraphicImageBean
public class StorageOutputHelperController implements Serializable {
public byte[] loadImageByByteArray(byte[] bytes) throws IOException {
LOGGER.info("START loadImageByByteArray");
try {
// Falls Null
if (bytes == null)
return Utils.toByteArray(Faces.getResourceAsStream("/resources/images/no-photo-icon.png"));
LOGGER.info("END loadImageByByteArray");
return bytes;
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getFullStackTrace(e));
return Utils.toByteArray(Faces.getResourceAsStream("/resources/images/no-photo-icon.png"));
}
}
}
previewPhotoboothProfileController.photoboothTemplate.photoboothCurrentSession.singlePictureOutput is already a byte array.
The problem is now, that the o:graphicImage will be updated, but only after 10 seconds and it the programm / thread is frozen.
It seems that the update from the p:remoteCommand for the h:form is the issue.
If I´m using <o:graphicImage dataURI="false" I cannot see this behaviour from the frozen window / programm but than the image is not showing.
Is this a known issue or is there something wrong from my end?
Edit:
Ok, it seems like the issue is in my SWT Browser screen which I open like this:
Display.getDefault().asyncExec(new Runnable() {
public void run() {
Display display = Display.getCurrent();
final Shell shell = new Shell(display);
Rectangle clientArea = Display.getCurrent().getClientArea();
shell.addListener(SWT.Traverse, new Listener() {
#Override
public void handleEvent(Event event) {
if (event.character == SWT.ESC) {
event.doit = false;
shell.close();
}
}
});
shell.setSize(500, 500);
shell.setFullScreen(false);
shell.setLocation(0, 0);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
shell.setLayout(gridLayout);
final Browser browser = new Browser(shell, SWT.NONE);
browser.setJavascriptEnabled(true);
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.verticalAlignment = GridData.FILL;
data.horizontalSpan = 3;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
data.widthHint = clientArea.width;
data.heightHint = clientArea.height;
browser.setLayoutData(data);
shell.open();
browser.setUrl("http://localhost:" + Constants.SERVER_PORT
+ "/portal/myurl);
}
});
Any idea, why the program is frozen?
If I check this in my local browser, it seems it´s working fine
the xaml code is:
<DockPanel>
<DataGrid x:Name="Quote" AutoGenerateColumns="False">
<DataGrid.Columns >
<DataGridTextColumn Header="Code" Binding="{Binding Code}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Quantities" Binding="{Binding Quantities}"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
<DataGridTextColumn Header="Cost" Binding="{Binding Cost}"/>
<DataGridCheckBoxColumn Header="Done" Binding="{Binding Done}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border BorderThickness="0" Background="Beige" Padding="10">
<StackPanel Orientation="Horizontal">
<Button Command="{Binding Path=DataContext.Renew1Command, RelativeSource= {RelativeSource FindAncestor,AncestorType=DataGrid}}">Call Command</Button>
</StackPanel>
</Border>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</DockPanel>
the model is:
public class StockModel
{
public event PropertyChangedEventHandler PropertyChanged;
public void propChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
private string code;
public string Code
{
get { return code; }
set
{
code = value;
this.propChanged("Code");
}
}
....
private bool done;
public bool Done
{
get { return done; }
set
{
done = value;
this.propChanged("Done");
}
}
public DelegateCommand Renew1Command;
public void Renew1(object obj)
{
MessageBox.Show("Hello Command");
System.Console.WriteLine("Hello Command");
}
public StockModel()
{
Renew1Command = new DelegateCommand();
this.Renew1Command.ExecuteAction += this.Renew1;
}
}
Finally i init the datasource by:
List<StockModel> stockList = new List<StockModel>();
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = true, Price = "16.1", Quantities = "200" });
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = false, Price = "16.1", Quantities = "100" });
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = true, Price = "16.1", Quantities = "100" });
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = true, Price = "16.1", Quantities = "200" });
this.Quote.ItemsSource = stockList;
the DataGridTextColumn can display the data of binding source,
but i can't click button of DataTemplate to call corresponding action from model.
how to fix the binding?
Call Command
move to StockModel class, replace "public DelegateCommand Renew1Command;" with "public DelegateCommand Renew1Command { get; set; }".
the problem will be solved.
I am quite new to Java so I need some help. I am trying to make a Notepad application.
The problem is that none of my menus or textfield is showing up. I cannot figure out what the problem is. Please help.
public class NotePad extends JFrame implements ActionListener {
private JTextArea txtArea;
private JMenuBar mnuBar;
private JMenu mnyFile, mnyFormat, mnyEdit, mnyHelp;
private JMenuItem openFile, saveFile, exit, textWrap, noTextWrap, clear, abtNotepad;
public NotePad() {
setTitle("NOTEPAD");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//Tekstboks
txtArea = new JTextArea();
//MenyBar
mnuBar = new JMenuBar();
//Meny
mnyFile = new JMenu("File");
mnyFormat = new JMenu("Format");
mnyEdit = new JMenu("Edit");
mnyHelp = new JMenu("Help");
//UnderMeny
openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
exit = new JMenuItem("Exit");
textWrap = new JMenuItem("Text Wrap");
noTextWrap = new JMenuItem("No Text Wrap");
clear = new JMenuItem("Clear");
abtNotepad = new JMenuItem("About Notepad");
add(txtArea);
add(mnuBar);
add(mnyFile);
add(mnyFormat);
add(mnyEdit);
add(mnyHelp);
add(openFile);
add(saveFile);
add(exit);
add(textWrap);
add(noTextWrap);
add(clear);
add(abtNotepad);
setJMenuBar(mnuBar);
setVisible(true);
}
public static void main(String[] args) {
new NotePad();
}
public void actionPerformed(ActionEvent e) {
}
}
Your constructor should look something like:
public NotePad() {
setTitle("NOTEPAD");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
txtArea = new JTextArea();
mnuBar = new JMenuBar();
mnyFile = new JMenu("File");
mnyFormat = new JMenu("Format");
mnyEdit = new JMenu("Edit");
mnyHelp = new JMenu("Help");
openFile = new JMenuItem("Open");
saveFile = new JMenuItem("Save");
exit = new JMenuItem("Exit");
textWrap = new JMenuItem("Text Wrap");
noTextWrap = new JMenuItem("No Text Wrap");
clear = new JMenuItem("Clear");
abtNotepad = new JMenuItem("About Notepad");
mnuBar.add(mnyFile);
mnuBar.add(mnyFormat);
mnuBar.add(mnyEdit);
mnuBar.add(mnyHelp);
mnyFile.add(openFile);
mnyFile.add(saveFile);
mnyFile.add(exit);
mnyFormat.add(textWrap);
mnyFormat.add(noTextWrap);
mnyEdit.add(clear);
mnyHelp.add(abtNotepad);
setJMenuBar(mnuBar);
add(txtArea);
setVisible(true);
}
Otherwise you are overriding each component you add to BorderLayout.
UPDATED:
I'm using a Detailsview Insert Control and I am trying to create a new row in the database from a checkbox in a CheckBoxList. I'm getting the "The INSERT statement conflicted with the FOREIGN KEY constraint"
Here is my cs:
protected void AddPrincipleStaffDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
CheckBoxList PrincipleStaffTitle = (CheckBoxList)FindControl("PrincipleStaffTitle");
if (PrincipleStaffTitle != null)
{
foreach (ListItem item in PrincipleStaffTitle.Items)
{
if (item.Selected)
{
string stringSession = Session["ProductionID"].ToString();
int intProductionID = Int32.Parse(stringSession);
var ID = item.Value;
using (var context = new FactoryTheaterModelFirstContainer())
{
PrincipleStaff principlestaff = new PrincipleStaff();
principlestaff.PrincipleStaffTitle = ID;
principlestaff.Production_proProductionID = intProductionID;
context.PrincipleStaffs.Add(principlestaff);
context.SaveChanges();
}
}
}
}
here is the aspx:
<asp:EntityDataSource ID="PrincipleStaffSource" runat="server" ConnectionString="name=FactoryTheaterModelFirstContainer" DefaultContainerName="FactoryTheaterModelFirstContainer" EnableFlattening="False" EntitySetName="PrincipleStaffs" EntityTypeFilter="PrincipleStaff" EnableInsert="True"></asp:EntityDataSource>
<asp:DetailsView ID="AddPrincipleStaffDetailsView" runat="server" AutoGenerateRows="False" DataKeyNames="PrincipleStaffID" DataSourceID="PrincipleStaffSource" Height="50px" Width="730px" DefaultMode="Insert" OnItemInserting="AddPrincipleStaffDetailsView_ItemInserting" OnItemInserted="AddPrincipleStaffDetailsView_ItemInserted">
<Fields>
<asp:TemplateField HeaderText="Add Principle Staff Role:" SortExpression="PrincipleStaffTitle">
<InsertItemTemplate>
<asp:CheckBoxList ID="PrincipleStaffTitle" runat="server" SelectedValue='<%# Bind("PrincipleStaffTitle") %>'>
<asp:ListItem Value="Director">Director(s)</asp:ListItem>
<asp:ListItem Value="Assistant Director">Assistant Director(s)</asp:ListItem>
<asp:ListItem Value="Written By">Written By(s)</asp:ListItem>
<asp:ListItem Value="Executive Producer">Executive Producer(s)</asp:ListItem>
<asp:ListItem Value="Producer">Producer(s)</asp:ListItem>
<asp:ListItem Value="Techincal Director">Technical Director(s)</asp:ListItem>
</asp:CheckBoxList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PrincipleStaffTitle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
The Production.proProductionID is the foreign key that I'm trying to get into the PrincipleStaff Production_proProductionid column. I can't seem to create the FK relationship.
Thanks for any help that can be provided!
The Below code works. I've since moved my context.savechanges(); outside the loop. My issue was happening on my ItemInserted command.
protected void AddPrincipleStaffDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
CheckBoxList PrincipleStaffCheckBox = (CheckBoxList)AddPrincipleStaffDetailsView.FindControl("PrincipleStaffTitleCheckBoxList");
if (PrincipleStaffCheckBox.Items.Count > 0)
{
foreach (ListItem item in PrincipleStaffCheckBox.Items)
{
if (item.Selected)
{
string stringSession = Session["ProductionID"].ToString();
int intProductionID = 0;
intProductionID = Int32.Parse(stringSession);
var principlestafftitle = item.Value;
try
{
using (var context = new FactoryTheaterModelFirstContainer())
{
Production proid = context.Productions.Single(i => i.proProductionID == intProductionID);
PrincipleStaff principlestaff = new PrincipleStaff()
{
PrincipleStaffTitle = principlestafftitle,
Production_proProductionID = intProductionID
};
proid.PrincipleStaffs.Add(principlestaff);
context.SaveChanges();
}
}
catch (Exception f)
{
Console.WriteLine(f); // or log to file, etc.
throw; // re-throw the exception if you want it to continue up the stack
}
}
}
}
}
My goal is to create a loginscreen for my users to log in to before entering the application.
I started using PRISM to control my modules, view, viewmodels, etc...
Now my problem is, i have a person class that looks like the folowing:
public class Person : DeletableSupport, IObserver, IDataErrorInfo, IActivatable
{
public class Properties
{
public const string Name = "Person_Name";
public const string SurName = "Person_SurName";
public const string Alias = "Person_Alias";
public const string Password = "Person_Password";
}
//TODO Add Rights bitarray type of thing
//TODO add IObserver stuff
private string _Name;
private string _SurName;
private string _Alias;
private string _Password;
private NotificationList _nList;
private ReminderList _rList;
private ProjectList _pList;
private DayPoolList _dpList;
private EventList _eList;
private ActivityList _aList;
[Transient]
private IActivator _activator;
#region Get/Set
public string Name
{
get
{
Activate(ActivationPurpose.Read);
return this._Name;
}
set
{
if (this._Name != value)
{
Activate(ActivationPurpose.Write);
this._Name = value;
}
}
}
public string Password
{
get
{
Activate(ActivationPurpose.Read);
return this._Password;
}
set
{
if (this._Password != value)
{
Activate(ActivationPurpose.Write);
this._Password = value;
RaisePropertyChanged(Person.Properties.Password);
}
}
}
public string SurName
{
get
{
Activate(ActivationPurpose.Read);
return this._SurName;
}
set
{
if (this._SurName != value)
{
Activate(ActivationPurpose.Write);
this._SurName = value;
}
}
}
public string Alias
{
get
{
Activate(ActivationPurpose.Read);
return this._Alias;
}
set
{
if (this._Alias != value)
{
Activate(ActivationPurpose.Write);
this._Alias = value;
RaisePropertyChanged(Person.Properties.Alias);
}
}
}
#endregion
public Person()
{
this._aList = new ActivityList();
this._dpList = new DayPoolList();
this._eList = new EventList();
this._nList = new NotificationList();
this._pList = new ProjectList();
this._rList = new ReminderList();
}
#region ListObjects
public ActivityList getActivityList()
{
Activate(ActivationPurpose.Read);
return this._aList;
}
public DayPoolList getDayPoolList()
{
Activate(ActivationPurpose.Read);
return this._dpList;
}
public EventList getEventList()
{
Activate(ActivationPurpose.Read);
return this._eList;
}
public NotificationList getNotificationList()
{
Activate(ActivationPurpose.Read);
return this._nList;
}
public ProjectList getProjectList()
{
Activate(ActivationPurpose.Read);
return this._pList;
}
public ReminderList getReminderList()
{
Activate(ActivationPurpose.Read);
return this._rList;
}
public ObservableCollectionEx<Activity> ActivityList
{
get
{
Activate(ActivationPurpose.Read);
return this._aList.getObsCollection();
}
set
{
if (this._aList.getObsCollection() != value)
{
Activate(ActivationPurpose.Write);
this._aList.setObsCollection(value);
}
}
}
public ObservableCollectionEx<Day> DayPoolList
{
get
{
Activate(ActivationPurpose.Read);
return this._dpList.getObsCollection();
}
set
{
if (this._dpList.getObsCollection() != value)
{
Activate(ActivationPurpose.Write);
this._dpList.setObsCollection(value);
}
}
}
public ObservableCollectionEx<Event> EventList
{
get
{
Activate(ActivationPurpose.Read);
return this._eList.getObsCollection();
}
set
{
if (this._eList.getObsCollection() != value)
{
Activate(ActivationPurpose.Write);
this._eList.setObsCollection(value);
}
}
}
public ObservableCollectionEx<Notification> NotificationList
{
get
{
Activate(ActivationPurpose.Read);
return this._nList.getObsCollection();
}
set
{
if (this._nList.getObsCollection() != value)
{
Activate(ActivationPurpose.Write);
this._nList.setObsCollection(value);
}
}
}
public ObservableCollectionEx<Project> ProjectList
{
get
{
Activate(ActivationPurpose.Read);
return this._pList.getObsCollection();
}
set
{
if (this._pList.getObsCollection() != value)
{
Activate(ActivationPurpose.Write);
this._pList.setObsCollection(value);
}
}
}
public ObservableCollectionEx<Reminder> ReminderList
{
get
{
Activate(ActivationPurpose.Read);
return this._rList.getObsCollection();
}
set
{
if (this._rList.getObsCollection() != value)
{
Activate(ActivationPurpose.Write);
this._rList.setObsCollection(value);
}
}
}
#endregion
#region IDataErrorInfo Members
private string m_error = string.Empty;
public string Error
{
set
{
m_error = value;
}
get
{
return m_error;
}
}
public string this[string columnName]
{
get
{
if (columnName == "Alias")
{
if (string.IsNullOrEmpty(Alias))
{
m_error = "Username cannot be empty";
return "Username cannot be empty";
}
}
if (columnName == "Password")
{
if (string.IsNullOrEmpty(Password))
{
m_error = "Password cannot be empty";
return "Password cannot be empty";
}
}
return "";
}
}
#endregion
public void Notify(object o, ObservedItem oType)
{
switch (oType)
{
case ObservedItem.oiNotification:
this._nList.Add((Notification)o);
break;
case ObservedItem.oiReminder:
this._rList.Add((Reminder)o);
break;
default:
break;
}
}
#region IActivatable Members
public void Activate(ActivationPurpose purpose)
{
if (_activator != null)
{
_activator.Activate(purpose);
}
}
public void Bind(IActivator activator)
{
if (_activator == activator)
{
return;
}
if (activator != null && null != _activator)
{
throw new System.InvalidOperationException();
}
_activator = activator;
}
#endregion
}
The code you see there is my person class, I implements INotifyPropertyChanged on basis of Josh Smits MVVMFoundation. The INotifyPropertyChanged is captured into the DeletableSupport. This class makes it so that if i delete and object in my db, the deleted flag is just set to true, instead of deleting it permantly.
The other code is for DB4O an object-database.
The following code is from my ViewModel:
public class LoginViewModel : ObservableObject
{
public LoginViewModel(Person person)
{
this.Person = person;
this.LoginCommand = new DelegateCommand<Person>(OnLoginExecuted, CanLoginExecute);
this.ResetCommand = new DelegateCommand<Person>(OnResetExecuted);
this.NewPersonCommand = new DelegateCommand<Person>(OnNewUserExecuted);
this.Person.PropertyChanged += new PropertyChangedEventHandler(LoginViewModel_PersonPropertyChanged);
}
void LoginViewModel_PersonPropertyChanged(object sender, PropertyChangedEventArgs e)
{
//Check after model change if command can be executed
((DelegateCommand<Person>)this.LoginCommand).RaiseCanExecuteChanged();
}
#region Get/Set
public ICommand LoginCommand
{
get;
set;
}
public ICommand ResetCommand
{
get;
set;
}
public ICommand NewPersonCommand
{
get;
set;
}
private Person _person;
public Person Person
{
get
{
return _person;
}
set
{
_person = value;
RaisePropertyChanged("Login_Person");
}
}
private string _error = "";
public string Error
{
get
{
return this._error;
}
set
{
this._error = value;
RaisePropertyChanged("Login_Error");
}
}
#endregion
void OnNewUserExecuted(Person p)
{
//Goto new user screen
}
void OnLoginExecuted(Person person)
{
if (person.Password != "Test" && person.Alias != "tce")
{
this.Error = "Incorrect login";
}
else
{
this.Error = "";
}
}
bool CanLoginExecute(Person person)
{
return this.Person.Alias != "" && this.Person.Password != "";
}
void OnResetExecuted(Person p)
{
p.Alias = string.Empty;
p.Password = string.Empty;
}
}
And the finaly my XAML.
<UserControl x:Class="GTV.Modules.Views.LoginView.View.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:GTV.Modules.Views.LoginView.View"
mc:Ignorable="d">
<Grid Name="MainGrid" Margin="5">
<Grid.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="5" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="Red"
FontSize="14" FontWeight="Bold">!</TextBlock>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder></AdornedElementPlaceholder>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Target="{Binding ElementName=Alias}" >
_Username:
</Label>
<TextBox Grid.Row="0" Grid.Column="1"
Name="Alias"
Text="{Binding Path=Person.Alias, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,ValidatesOnDataErrors=True}"/>
<Label Grid.Row="1" Grid.Column="0" Target="{Binding ElementName=Password}" >
_Password:
</Label>
<TextBox Grid.Row="1" Grid.Column="1"
Name="Password"
Text="{Binding Path=Person.Password,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,ValidatesOnDataErrors=True}"/>
<TextBlock Grid.ColumnSpan="2" Grid.Row="4" Text="{Binding Path=Error,Mode=TwoWay}" Foreground="Red"></TextBlock>
<Button Command="{Binding Path=LoginCommand}"
CommandParameter="{Binding Path=Person}"
Grid.Column="0" Grid.Row="2"
>Login</Button>
<Button Command="{Binding Path=ResetCommand}"
CommandParameter="{Binding Path=Person}"
Grid.Column="1" Grid.Row="2"
>Reset</Button>
<Button Command="{Binding Path=NewPersonCommand}"
Grid.Column="1" Grid.Row="3"
>New user</Button>
</Grid>
</Border>
</Grid></UserControl>
XAML.cs looks like this:
public partial class LoginView : UserControl, IView
{
public LoginView()
{
InitializeComponent();
MainGrid.DataContext = new LoginViewModel(new Person()
{
Alias = "Enter username",
Password = "123"
});
;
}
public LoginViewModel ViewModel
{
get
{
return this.DataContext as LoginViewModel;
}
}
}
My problem is that when i press the reset button, my Person property in my viewmodel gets updated and notifies the change, but the text in my textbox doens't update.
I have an example module where it does work and i can't seem to find my problem.
( http://blog.projectsoftware.ro/2009/09/wpf-login-demo-with-mvvm-pattern-update/ )
Anybody any clue what i'm doing wrong... I converted the example to 4.0 and it is still working.. it drives me insane to be honest...
Big thanks in advance!
It looks like you are missing a call to RaisePropertyChanged(Person.Properties.<Name>); in both your SurName and Name properties.
Update:
After clarification and a second look, the problem seems to be the Properties class inside Person. It has to be like this:
public class Properties
{
public const string Name = "Name";
public const string SurName = "SurName";
public const string Alias = "Alias";
public const string Password = "Password";
}
I removed the "Person_" prefix, so the consts contain exactly the properties names