All fields are marked as TwoWay on databinding, but its obvious I have something wrong. What I have is a page showing a view to add new Devices on one side of the view, and a list of Devices on the other side.. What I'm trying to do is when selecting an item from listview, it will update values within the TextBox for viewing and editing purposes.
The Save option (not shown in Code Below) currently works when I create a new Device, and will refresh the list. however, right now I'm Going back a Frame when complete. What I would like to do is refresh ListView when I click save.
Values from XAML page
<TextBox PlaceholderText="Host Name" Text="{x:Bind ViewModel.HostName, Mode=TwoWay}" Name="hostNameTB" AcceptsReturn="True" />
<TextBox PlaceholderText="Drive Model" Text="{x:Bind ViewModel.DriveModel, Mode=TwoWay}" Name="driveModelTB" />
<TextBox PlaceholderText="Drive SN" Text="{x:Bind ViewModel.DriveSN, Mode=TwoWay}" Name="driveSNTB" AcceptsReturn="True" InputScope="Digits"/>
Code from ViewModel
private Device _ActiveDevice;
private int _HostName;
//All Variables Created for DataBinding to XAML page
//public int HostName { get { return _HostName; } set { _ActiveDevice.HostName = value; } }
public int HostName { get; set; }
public string DriveModel { get; set; }
public string DriveSN { get; set; }
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
{
Value = (suspensionState.ContainsKey(nameof(Value))) ? suspensionState[nameof(Value)]?.ToString() : parameter?.ToString();
await Task.CompletedTask;
var uri = new Uri("http://localhost:2463/api/Devices");
HttpClient client = new HttpClient();
try
{
var JsonResponse = await client.GetStringAsync(uri);
var devicesResult = JsonConvert.DeserializeObject<List<Device>>(JsonResponse);
Devices = devicesResult;
_ActiveDevice = JsonConvert.DeserializeObject<List<Device>>(JsonResponse)[0];
}
catch
{
MessageDialog dialog = new MessageDialog("Unable to Access WebService at this Time!");
await dialog.ShowAsync();
}
//client.Dispose();
}
public void deviceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var device = ((sender as ListView).SelectedItem as Device);
_ActiveDevice = device;
HostName = device.HostName;
DriveModel = device.DriveModel;
DriveSN = device.DriveSN;
}
You have to inherit the view model from INotifyPropertyChanged to let the binding know there was an update of the value.
public class MainViewModel: INotifyPropertyChanged
{
public int HostName { get => hostName; set { hostName = value; OnPropertyChanged("HostName"); } }
private int hostName;
...
void OnPropertyChanged(String prop)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Update your binding (and all the others) to this:
<TextBox PlaceholderText="Host Name" Text="{Binding ViewModel.HostName, UpdateSourceTrigger=PropertyChanged}" Name="hostNameTB" AcceptsReturn="True" />
I was way off.. my fix was more of what Sir Rufo proposed.. The XAML was correct, but I needed to set the Get and Set of the property to update the property, and then to make sure selected device was update each property.
private int _HostName;
public int HostName { get { return _HostName; } set { Set(ref _HostName, value); } }
public void deviceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var device = ((sender as ListView).SelectedItem as Device);
//_ActiveDevice = device;
HostName = device.HostName;
DriveModel = device.DriveModel;
DriveSN = device.DriveSN;
Related
I'm having troubles with my UI not updating when my properties change, even though I have INotifyPropertyChange applied. When i run the code the first time, it shows up correct and the UI is updated. While debbuging I can see the new values being set to the strings of the viewmodel and that the OnPropertChange event is fired, it just don't happen anything in the UI. The code below will be in order of events. As extra information, I use the same code to update the viewmodel both in the first and second time.
public partial class Transaktioner : Window
{
ViewModelCommon.ViewModel view = new ViewModelCommon.ViewModel();
private static List<ViewModelCommon.Items2> getAccountingRowsListEdited = new List<ViewModelCommon.Items2>();
{
DataContext = view;
InitializeComponent();
}
private async Task GetAccountinTransactionsAsync()
{
await Task.Run(() =>
{
getAccountingRowsList = client.GetAccountingTransactions(ftglist[index], 0, ref status).ToList();
foreach (var v in getAccountingRowsList)
{
getAccountingRowsListEdited.Add(new ViewModelCommon.Items2
{
itemName2 = v.ver.ToString(),
value2 = v.text,
vertyp = v.vtyp,
s2 = v.kto.ToString(),
s3 = v.trdat.ToString()
});
}
Task.Run(async () =>
{
await SearchAndDisplayResult();
});
});
}
private async Task SearchAndDisplayResult(int exclusion = 0)
{
await Task.Run(() =>
{
var verfikationer = getAccountingRowsListEdited.Where(u => u.vertyp != exclusion).Count(u => u.s2.ToString().Equals("0"));
view.VerifikationerTotal = verfikationer.ToString();
});
}
The ViewModel:
class ViewModelCommon
{
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName]string propertyName = null)
{
if (!EqualityComparer<T>.Default.Equals(field, newValue))
{
field = newValue;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
return false;
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected void Test(string sb)
{
Transaktioner tr = new Transaktioner("");
tr.ExcludeStringChanged(sb);
}
}
public class ViewModel : ViewModelBase
{
private string _verifikationerTotal;
public string VerifikationerTotal
{
get { return _verifikationerTotal; }
set
{
if (value != _verifikationerTotal)
{
_verifikationerTotal = value;
OnPropertyChanged("VerifikationerTotal");
}
}
}
private string _ExcludeString;
public string ExcludeString
{
get { return _ExcludeString; }
set
{
if (value != _ExcludeString)
{
_ExcludeString = value;
OnPropertyChanged("ExcludeString");
Test(ExcludeString);
}
}
}
}
The WPF:
<TextBox x:Name="TextBoxVerifikationerTotal" Text="{Binding VerifikationerTotal}" IsEnabled="False" HorizontalAlignment="Left" Height="23" Margin="583,182,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="99"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="837,10,0,0" TextWrapping="Wrap" Text="{Binding Path=ExcludeString, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" VerticalAlignment="Top" Width="286"/>
The code above works as expected.
In the UI there is an option to introduce the optional attribute to exclude values. Those are bound to the "ExludeString" this also works and fires the event passing it again to the SearchAndDisplayResult(int exclusion = 0) with the replaces value of the int being passed. While debugging I can see that the event can successfully find a new value and passing it to the ViewModel, but it doesn't update the UI.
Are there any thoughts on why the UI is not updated? Thank you in advance!
The code has been shortend to show the vitals
Answer for this case was the
ViewModelCommon.ViewModel view = new ViewModelCommon.ViewModel();
not being set to a private static while working with Tasks.
Currently, in order for my textboxes to update, i need to navigate away from my SettingsPage and then back into it to see the changes in the TextBoxes.
Would you be able to help with getting these TextBoxes to update when the globalvariable changes? I have looked into using INotifyPropertyChanged. Im just not sure how best to implement it
Here is the code i have currently. its very basic.
Settings page XAML
<Frame Background="{StaticResource CustomAcrylicDarkBackground}">
<StackPanel>
<TextBox Width="500" Header="File Name" IsReadOnly="True" Foreground="White" Text="{x:Bind TextBoxFileName}"/>
<TextBox Width="500" Header="File Location" IsReadOnly="True" Foreground="White" Text="{x:Bind TextBoxFilePath}"/>
</StackPanel>
</Frame>
Code Behind
using static BS.Data.GlobalVariableStorage;
namespace BS.Content_Pages
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class SettingsPage : Page
{
public SettingsPage()
{
this.InitializeComponent();
}
public string TextBoxFilePath = GlobalVariables.FilePath;
public string TextBoxFileName = GlobalVariables.FileName;
}
}
}
GlobalVariablesStorage Class
namespace BS.Data
{
class GlobalVariableStorage
{
public static class GlobalVariables
{
public static string FilePath { get; set; }
public static string FileName { get; set; }
}
}
}
Save File Function within MainPage.XAML.cs (Parses the save name to GlobalVariableStorage)
public async void SaveButton_ClickAsync(object sender, RoutedEventArgs e)
{
SaveFileClass instance = new SaveFileClass();
IStorageFile file = await instance.SaveFileAsync();
if (file != null)
{
GlobalVariables.FileName = file.Name;
GlobalVariables.FilePath = file.Path;
// Debugging the output file paths
// Remember to REMOVE
Debug.WriteLine(GlobalVariables.FileName);
Debug.WriteLine(GlobalVariables.FilePath);
WriteFile.WriteFileData();
}
}
The main issue is here is that you somehow need to tell your view when to refresh the data-bound values. And for you to be able to do this you need to know when this happens.
In other words, the GlobalVariables class should raise an event whenever any property is set to a new value. It could for example raise the built-in PropertyChanged event:
public static class GlobalVariables
{
private static string _filePath;
public static string FilePath
{
get { return _filePath; }
set { _filePath = value; NotifyPropertyChanged(); }
}
private static string _fileName;
public static string FileName
{
get { return _fileName; }
set { _fileName = value; NotifyPropertyChanged(); }
}
public static event PropertyChangedEventHandler PropertyChanged;
private static void NotifyPropertyChanged([CallerMemberName]string propertyName = "") =>
PropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
In your view you could then subscribe to this event and raise another event that the view handles. You tell the view update a data-bound value by implementing the INotifyPropertyChanged interface and raise the PropertyChanged event for the property to be updated. Something like this:
public sealed partial class SettingsPage : Page, INotifyPropertyChanged
{
public SettingsPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
GlobalVariables.PropertyChanged += GlobalVariables_PropertyChanged;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
GlobalVariables.PropertyChanged -= GlobalVariables_PropertyChanged;
}
private void GlobalVariables_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(GlobalVariables.FilePath):
NotifyPropertyChanged(nameof(TextBoxFilePath));
break;
case nameof(GlobalVariables.FileName):
NotifyPropertyChanged(nameof(TextBoxFileName));
break;
}
}
public string TextBoxFilePath => GlobalVariables.FilePath;
public string TextBoxFileName => GlobalVariables.FileName;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
Also note that the default mode of x:Bind is OneTime, so you should explicitly set the Mode to OneWay in the view, e.g.:
Text="{x:Bind TextBoxFilePath, Mode=OneWay}"
I have a WPF application that has multiple comboboxes and buttons. I am learning the MVVM model with this application. The first combobox will display a list of database instances. This is done at the start of the application. This works fine.
There is a button object next to the database instances combobox. When the user clicks this button I need to get the contents of the database instance combobox and use it in a call to get all the databases in that instance. I am using a RelayCommand (ICommand) for the actions. The action for the button is getting setup correctly. I have a method SelectedDatabase in the DBInstance class but it is null when I click the button.
In the LoadDBInfo method below the selectedItem parameter is null.
Here is my XAML:
<ComboBox x:Name="cbxRLFDBInstances" ItemsSource="{Binding DBInstances}"
SelectedValue="{Binding SelectedDBInstance}" SelectedValuePath="value"
HorizontalAlignment="Left" Height="28" Margin="189,87,0,0" VerticalAlignment="Top"
Width="250" FontFamily="Arial" FontSize="14.667"
IsEditable="True"/>
<Button x:Name="btnRLFDBLoadDBInfo" Content="Load DB Info" Command="{Binding LoadDBInfoCommand}"
CommandParameter="{Binding SelectedDBInstance}" HorizontalAlignment="Left" Height="26" Margin="475,89,0,0" VerticalAlignment="Top"
Width="101" FontFamily="Arial" FontSize="14.667" Background="#FFE8F9FF"
ToolTip="Click here after choosing or typing in the datbase instance. This will populate the database list."/>
<ComboBox x:Name="cbxRLFDBName" HorizontalAlignment="Left" Height="28" Margin="189,132,0,0"
ItemsSource="{Binding DBDatabases}" SelectedValue="{Binding SelectedDBDatabase}"
SelectedValuePath="value" VerticalAlignment="Top" Width="250" FontFamily="Arial"
FontSize="14.667" IsEditable="True" IsReadOnly="True"
ToolTip="Once a database is choosen the table list will automatically be populated."/>
Here is my ViewModel:
namespace DatabaseTest.ViewModel
{
class RLFDatabaseTableViewModel
{
Utilities dbtUtilities = new Utilities();
public RelayCommand LoadDBInfoCommand
{
get;
set;
}
public RLFDatabaseTableViewModel()
{
LoadDBInstances();
LoadDBInfoCommand = new RelayCommand(LoadDBInfo);
}
#region Database Instance
public IList<DBInstance> DBInstances
{
get;
set;
}
public void LoadDBInstances()
{
IList<DBInstance> dbInstances = nList<DBInstance>();
DataTable dt = SmoApplication.EnumAvailableSqlServers(false);
dbInstances.Add(new DBInstance { DBInstanceName = "fal-conversion\\mun2012ci" });
dbInstances.Add(new DBInstance { DBInstanceName = "fal-conversion\\mun2014ci" });
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
dbInstances.Add(new DBInstance { DBInstanceName = dr["Name"].ToString() });
}
}
DBInstances = dbInstances;
}
#endregion Database Instance
#region Database Names
public IList<DBDatabase> DBDatabases
{
get;
set;
}
public void LoadDBDatabases()
{
IList<DBDatabase> dbDatabases = new List<DBDatabase>();
dbDatabases.Add(new DBDatabase { DBDatabaseName = "DB - A" });
dbDatabases.Add(new DBDatabase { DBDatabaseName = "DB - B" });
DBDatabases = dbDatabases;
}
#endregion Database Names
#region Button Cammands
void LoadDBInfo(object selectedItem)
{
SqlConnection sqlConn = null;
IList<DBDatabase> dbDatabaseNames = new List<DBDatabase>();
// string selectedItem = dbInstances.
//Setting the PUBLIC property 'TestText', so PropertyChanged event is fired
if (selectedItem == null)
dbDatabaseNames = null;
else
{
SelectedDBInstance = selectedItem as DBInstance;
dbDatabaseNames = dbtUtilities.GetDBNames(sqlConn, _selectedDBInstance.ToString(),
_selectedDBDatabase.ToString());
}
DBDatabases = dbDatabaseNames;
}
#endregion Button Commands
}
Here is my Model:
namespace DatabaseTest.Model
{
public class RLFDatabaseTableModel { }
public class DBInstance : INotifyPropertyChanged
{
private string strDBInstance;
public override string ToString()
{
return strDBInstance;
}
public string DBInstanceName
{
get
{
return strDBInstance;
}
set
{
if (strDBInstance != value)
{
strDBInstance = value;
RaisePropertyChanged("DBInstanceName");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
public class DBDatabase : INotifyPropertyChanged
{
private string strDBDatabase;
public override string ToString()
{
return strDBDatabase;
}
public string DBDatabaseName
{
get
{
return strDBDatabase;
}
set
{
if (strDBDatabase != value)
{
strDBDatabase = value;
RaisePropertyChanged("DBDatabaseName");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
EDIT: This is my code to load the 2nd combobox, cbxRLFDBName, The DBDatabase has the values but the combobox is not loaded.
public void LoadDatabases(string strDBInstanceName)
{
string strQuery;
IList<DBDatabase> dbDatabases = new List<DBDatabase>();
SqlConnection sqlUtilDBConn = null;
try
{
if (sqlUtilDBConn != null)
{
sqlUtilDBConn.Close();
}
sqlUtilDBConn = dbtUtilities.LoginToDatabase(strDBInstanceName, "master");
strQuery = "select name from sys.databases order by 1";
using (SqlCommand sqlCmd = new SqlCommand(strQuery, sqlUtilDBConn))
{
SqlDataReader sqlDataRead = sqlCmd.ExecuteReader();
while (sqlDataRead.Read())
{
string strDBNme = sqlDataRead.GetString(0);
dbDatabases.Add(new DBDatabase { DBDatabaseName = strDBNme });
}
sqlDataRead.Close();
sqlCmd.Dispose();
}
}
catch (Exception exQuery)
{
string strMsg;
strMsg = "GetNumRows: Error, '" + exQuery.Message + "', has occurred.";
System.Windows.MessageBox.Show(strMsg);
}
DBDatabases = dbDatabases;
}
EDIT: I have removed some of the code that is not needed in the hopes that this will be easier to read. My issue is that combobox "cbxRLFDBInstances" with ItemsSource="{Binding DBInstances}" loads the combobox fine. I also have another combobox, "cbxRLFDBName" with ItemsSource="{Binding DBDatabases}". When I choose the appropriate database instance and click the Load DB Info button, LoadDatabases runs and I can see that DBDatabases has the information needed in it. However the combobox is not loaded and I do not have a failure. Why does one ItemsSource data binding work and the other does not? I believe I am setting the class correctly but it seems lo=ike the binding is not happening? What have I missed?
Your code look fine to me, except for the SelectedValuePath="value" on the ComboBoxes. SelectedValuePath specifies a property on the selected item that is to be bound to the SelectedValue. SelectedDBInstance is of type DBInstance and DBInstance class does not define a value property, so I'd say you just have to remove SelectedValuePath="value" from the ComboBoxes.
Edit:
You need your ViewModel to implement INotifyPropertyChanged:
class RLFDatabaseTableViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
// the rest of RLFDatabaseTableViewModel implementation ...
}
And then every time you change a property value inside ViewModel, you also need to call RaisePropertyChanged immediately after. For example:
DBDatabases = dbDatabaseNames;
RaisePropertyChanged("DBDatabases");
It is helpful to define your properties like so:
public string StringProperty
{
get { return this.stringProperty; }
set {
this.stringProperty = value;
this.RaisePropertyChanged("StringProperty");
}
}
private string stringProperty;
Then you can just write
this.StringProperty = "new value";
and the new value will be set and a change notification sent.
You have to send the notifications because the View (XAML) and ViewModel are different classes and the View has no way of knowing that a property on the ViewModel has changed. If ViewModel implements INotifyPropertyChanged, WPF will listen for property changes through the PropertyChanged event and update the View accordingly.
Have you tried to pass command parameter as selected izem from combobox, something like:
CommandParameter="{Binding SelectedItem,ElementName=yourComboBoxName}"
Is it possible to have one ViewModel for multiple dynamic Tabs? Meaning that, whenever I create a new tab, it should use the same instance of ViewModel so I can retrieve information and also prevent each Tab from sharing data/showing the same data.
The setting I'm thinking of using it in would be for a payroll application where each employee's payslip can be updated from each tab. So the information should be different in each Tab.
Is this possible?
Update: Added code
MainViewModel where Tabs Collection is handled:
public ObservableCollection<WorkspaceViewModel> Workspaces { get; set; }
public MainViewModel()
{
Workspaces = new ObservableCollection<WorkspaceViewModel>();
Workspaces.CollectionChanged += Workspaces_CollectionChanged;
}
void Workspaces_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null && e.NewItems.Count != 0)
foreach (WorkspaceViewModel workspace in e.NewItems)
workspace.RequestClose += this.OnWorkspaceRequestClose;
if (e.OldItems != null && e.OldItems.Count != 0)
foreach (WorkspaceViewModel workspace in e.OldItems)
workspace.RequestClose -= this.OnWorkspaceRequestClose;
}
private void OnWorkspaceRequestClose(object sender, EventArgs e)
{
CloseWorkspace();
}
private DelegateCommand _exitCommand;
public ICommand ExitCommand
{
get { return _exitCommand ?? (_exitCommand = new DelegateCommand(() => Application.Current.Shutdown())); }
}
private DelegateCommand _newWorkspaceCommand;
public ICommand NewWorkspaceCommand
{
get { return _newWorkspaceCommand ?? (_newWorkspaceCommand = new DelegateCommand(NewWorkspace)); }
}
private void NewWorkspace()
{
var workspace = new WorkspaceViewModel();
Workspaces.Add(workspace);
SelectedIndex = Workspaces.IndexOf(workspace);
}
private DelegateCommand _closeWorkspaceCommand;
public ICommand CloseWorkspaceCommand
{
get { return _closeWorkspaceCommand ?? (_closeWorkspaceCommand = new DelegateCommand(CloseWorkspace, () => Workspaces.Count > 0)); }
}
private void CloseWorkspace()
{
Workspaces.RemoveAt(SelectedIndex);
SelectedIndex = 0;
}
private int _selectedIndex = 0;
public int SelectedIndex
{
get { return _selectedIndex; }
set
{
_selectedIndex = value;
OnPropertyChanged("SelectedIndex");
}
}
WorkspaceViewModel:
public PayslipModel Payslip { get; set; }
public WorkspaceViewModel()
{
Payslip = new PayslipModel();
SaveToDatabase = new DelegateCommand(Save, () => CanSave);
SelectAll = new DelegateCommand(Select, () => CanSelect);
UnSelectAll = new DelegateCommand(UnSelect, () => CanUnSelect);
}
public ICommand SaveToDatabase
{
get; set;
}
private bool CanSave
{
get { return true; }
}
private async void Save()
{
try
{
MessageBox.Show(Payslip.Amount.ToString());
}
catch (DbEntityValidationException ex)
{
foreach (var en in ex.EntityValidationErrors)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}, {1}", en.Entry.Entity.GetType().Name, en.Entry.State) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
foreach (var ve in en.ValidationErrors)
{
exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}, {1}", ve.PropertyName, ve.ErrorMessage) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
}
catch (Exception ex)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}", ex) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
public event EventHandler RequestClose;
private void OnRequestClose()
{
if (RequestClose != null)
RequestClose(this, EventArgs.Empty);
}
private string _header;
public string Header
{
get { return _header; }
set
{
_header = value;
OnPropertyChanged("Header");
}
}
Payroll UserControl where WorkspaceViewModel is DataContext:
public Payroll()
{
InitializeComponent();
DataContext = new WorkspaceViewModel();
}
Payroll.xaml Tabcontrol:
<dragablz:TabablzControl ItemsSource="{Binding Workspaces}" SelectedIndex="{Binding SelectedIndex}" BorderBrush="{x:Null}">
<dragablz:TabablzControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"/>
</DataTemplate>
</dragablz:TabablzControl.ItemTemplate>
<dragablz:TabablzControl.ContentTemplate>
<DataTemplate>
<ContentControl Margin="16">
<local:TabLayout DataContext="{Binding Path=Payslip, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" x:Name="tabLayout"/>
</ContentControl>
</DataTemplate>
</dragablz:TabablzControl.ContentTemplate>
</dragablz:TabablzControl>
This works as expected, each tab displays different info and bindings work okay. However, I'm unable to retrieve the info in the MessageBox.
I'm not sure if I totally understand your question but if you need a Window with a tabcontrol, in which each tab refers to an employee, then you will have to bind the ItemsSource of the tabcontrol to a list of the ViewModel.
It is not possible to bind all tabpages to the same instance because then the tabpages will all do the same, and show the same information.
I couldn't get it to work the way I had it, so I placed the save button inside the view that has DataContext set to where employee's info are loaded and got it to work from there, since it directly accesses the properties.
ViewModels should have a 1:1 relationship with the model. In your TabControl's DataContext, let's say you have properties like:
public ObservableCollection<EmployeeViewModel> Employees {get;set;}
public EmployeeViewModel CurrentEmployee
{
get { return _currentEmployee;}
set
{
_currentEmployee = value;
OnPropertyChanged("CurrentEmployee");
}
}
where Employees is bound to ItemsSource of the TabControl, and CurrentEmployee to CurrentItem. To create a new tab:
var employee = new Employee();
var vm = new EmployeeViewModel(employee);
Employees.Add(vm);
CurrentEmployee = vm;
If you want a save button outside of the TabControl, just set its DataContext to CurrentEmployee.
I hope this helps!
Edit:
Two things I think are causing problems:
Payroll.xaml should be bound to MainViewModel since that's where the Workspaces collection is.
Do not instantiate ViewModels in your view's code behind. Use a DataTemplate instead (see this question).
Take a look at Josh Smith's MVVM demo app (source code)
When I click a button from my form and enter a string into the Textbox that appears, I need it to create a new ObservableCollection list with that string name.
A label with the string name will then appear on the form. We then have created a ContextMenu for that label. From here you can add another string inside that list that you clicked on.
This is the save button for the string:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
string titleName = txtTitle.Text;
_viewmodel.RenameTitle(titleName);
_Addtitle.Execute(null);
this.Close();
}
Then we go into the ViewModel class(titleName is the first string entered).
public ICommand AddTitleCommand { get; set; }
public ViewModel()
{
this.AddTitleCommand = new RelayCommand(new Action<object>((o) => OnAddTitle()));
}
private void OnAddTitle()
{
NewTitle += titleName;
}
The OnAddTitle() method and the two below are where the problem arises. At the moment, the titleName string is split up, and each character is displayed as a new collection (we are assuming) on the form, instead of a title being one collection. - There should be multiple titles with multiple collections, each title having its own collection. The title should be as one word instead of being split up into individual characters.
public string NewTitle
{
get { return newTitle; }
set { newTitle = value; OnPropertyChanged(() => NewTitle); }
}
public void AddCollection()
{
ObservableCollection<string> collection = new ObservableCollection<string>();
collection.Add(NewTitle);
Collections.Add(collection);
}
XAML code on the form binding an ItemsSource to the NewTitle property:
<StackPanel Name="Content" Margin="0,99,0,0">
<TextBox x:Name="txtname" Height="23" TextWrapping="Wrap" Margin="200,0,500,0"/>
<ListView x:Name="TitleList" ItemsSource="{Binding NewTitle}" ItemTemplate="{DynamicResource Template}" BorderBrush="{x:Null}">
</ListView>
</StackPanel>
EDIT:
This is what it looks like, it should appear as one string in a collection.
I highly recommend you continue researching MVVM because you'll want to change how you are using WPF. But I think your problem here is around the fact you are using an
ObservableCollection<ObservableCollection<string>>
when what you really need is something like this:
public class YourTitleClass : INotifyPropertyChanged
{
private string _title;
public string Title
{
get { return _title; }
set
{
if (_title.Equals(value))
return;
_title = value;
RaisePropertyChanged(() => Title);
}
}
private ICollection<string> _subtitles;
public ICollection<string> Subtitles
{
get
{
if (_subtitles == null)
_subtitles = new ObservableCollection<string>();
return _subtitles;
}
set
{
if (value == _subtitles)
return;
_subtitles = value;
RaisePropertyChanged(() => Subtitles);
}
}
public YourTitleClass(string title)
{
_title = title;
}
}
Then your AddCollection method needs to add an instance of this class to the ViewModels collection
public void AddCollection()
{
YourTitleClass newTitleClass = new YourTitleClass(NewTitle);
Collections.Add(newTitleClass);
}
Make sure you change the type of Collections in your ViewModel to
ObservableCollection<YourTitleClass>
Now when you add a "subtitle" (or whatever this subcollection represents), you'll add it to YourTitleClass.Subtitles. And if you are using the parent level of the object it's YourTitleClass.Title.