Passing two parameters in Command biding in WPF and MVVM
-
Hi Friends, I have two Textboxes, One for Id and another for the Name, I need to pass both of these two into MyViewModel class Id and Name Properties in a Button Click, so far I am able to pass one of them to the ViewModel, how can I pass both the parameters to the ViewModel, any help is greatly helpful. Thanks in advance please. So far I am able to do as below in View
In the Command class
public void Execute(object parameter)
{
var values = (string)parameter;this.ViewModel.AddAnItem(values); }
In View Model
public void AddAnItem(string \_name) { int t; if ((\_personNames == null) || (\_personNames.Count() <= 0)) t = 1; else t = (\_personNames.Count <= 0) ? 1 : \_personNames.Max(x => x.Id) + 1; \_personNames.Add(new TestData(t, \_name)); }
I want to change this AddAnItem to take two parameters int and string and if possible return value also, but return is not important, what is important is to take two parameters. Please need help, any sort of help a link or code snippet or even a suggestion also helps.
Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
Why? Just put two string properties in your ViewModel and binding your Text boxes to them. You can then read them from your command.
This space for rent
-
Why? Just put two string properties in your ViewModel and binding your Text boxes to them. You can then read them from your command.
This space for rent
The condition in the company is to bind them from xaml but when I am trying to bind the ViewModel from xaml, I am getting value only if I bind one but when I am trying to bind the second one and try to raise NotifyPropertyChanged event then both the values are not coming inth xaml. Then I tried add static resource of ViewModel in xaml, the ViewModel has the same namespace and assembly as xaml view file, its not letting me add the static resource of the ViewModel. Without ViewModel is not being accessed as StaticResource I am not able to reference the ViewModels Properties in the View, its giving me compile error saying that ViewModel is not available in the namespace. I tried it many ways not not helping me out, I wrote the code as above question. Anyhelp please.
Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
-
The condition in the company is to bind them from xaml but when I am trying to bind the ViewModel from xaml, I am getting value only if I bind one but when I am trying to bind the second one and try to raise NotifyPropertyChanged event then both the values are not coming inth xaml. Then I tried add static resource of ViewModel in xaml, the ViewModel has the same namespace and assembly as xaml view file, its not letting me add the static resource of the ViewModel. Without ViewModel is not being accessed as StaticResource I am not able to reference the ViewModels Properties in the View, its giving me compile error saying that ViewModel is not available in the namespace. I tried it many ways not not helping me out, I wrote the code as above question. Anyhelp please.
Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
Without you showing your ViewModel and View here, it's impossible to say why it doesn't work, however I would take a guess that you aren't actually referencing the namespace in your XAML. Typically, I would expect to see something like this in your Window/UserControl header:
xmlns:vm="clr-namespace:MyApplication.ViewModelNamespace"
Then you would reference this as such
<Window.DataContext>vm:NameOfViewModel</Window.Resources>
This space for rent
-
Without you showing your ViewModel and View here, it's impossible to say why it doesn't work, however I would take a guess that you aren't actually referencing the namespace in your XAML. Typically, I would expect to see something like this in your Window/UserControl header:
xmlns:vm="clr-namespace:MyApplication.ViewModelNamespace"
Then you would reference this as such
<Window.DataContext>vm:NameOfViewModel</Window.Resources>
This space for rent
Hi, My here is my View
And ViewModel
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace TestWPFApplication
{
public class TestViewModel : INotifyPropertyChanged
{
TestData _testData;//public TestCommand TestCommand { get; set; } public TestSpecificCommand TestSpecificCom
-
Hi, My here is my View
And ViewModel
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace TestWPFApplication
{
public class TestViewModel : INotifyPropertyChanged
{
TestData _testData;//public TestCommand TestCommand { get; set; } public TestSpecificCommand TestSpecificCom
TestViewModel is not a resource, yet you are attempting to use it as though it is. Change your
Window.Resources
section to aWindow.DataContext
that looks like this<Window.DataContext>
<testns:TestViewModel />
</Window.DataContext>This space for rent
-
TestViewModel is not a resource, yet you are attempting to use it as though it is. Change your
Window.Resources
section to aWindow.DataContext
that looks like this<Window.DataContext>
<testns:TestViewModel />
</Window.DataContext>This space for rent
Hi, I changed it as below, but still it is giving me the same error, any help please, still it is saying me that and in the UI it is saying invalid xaml.
Error 1 The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication
My code is below, any help please?
Now I changed a little bit the ViewModel I put in a folder called ViewModel and Model in Model folder and changed their namespaces and changed the reference of clr-namespace as well but still the error persists.
Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
-
Hi, I changed it as below, but still it is giving me the same error, any help please, still it is saying me that and in the UI it is saying invalid xaml.
Error 1 The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication
My code is below, any help please?
Now I changed a little bit the ViewModel I put in a folder called ViewModel and Model in Model folder and changed their namespaces and changed the reference of clr-namespace as well but still the error persists.
Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
And that would be correct. Look at the code you posted. The ViewModel isn't in that namespace, it's in TestWPFApplication. The error was very explicit about what the problem was.
This space for rent
-
And that would be correct. Look at the code you posted. The ViewModel isn't in that namespace, it's in TestWPFApplication. The error was very explicit about what the problem was.
This space for rent
Thanks for all your help but I found the Problem that I was struggling with, it was because of the network drive I loading the Application path from. When we are using loading the static resources data context the UI can take them only if they from trusted source, my application was on network drive. Thanks for all your support Pete and Rick, without you guys I couldn't have got this far. About my wrong code here I was changing the code all the time, may be pasted it when it was wrong. Thank you very much for that.
Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
-
Thanks for all your help but I found the Problem that I was struggling with, it was because of the network drive I loading the Application path from. When we are using loading the static resources data context the UI can take them only if they from trusted source, my application was on network drive. Thanks for all your support Pete and Rick, without you guys I couldn't have got this far. About my wrong code here I was changing the code all the time, may be pasted it when it was wrong. Thank you very much for that.
Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
Glad you got there in the end.
This space for rent
-
Hi, I changed it as below, but still it is giving me the same error, any help please, still it is saying me that and in the UI it is saying invalid xaml.
Error 1 The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication
My code is below, any help please?
Now I changed a little bit the ViewModel I put in a folder called ViewModel and Model in Model folder and changed their namespaces and changed the reference of clr-namespace as well but still the error persists.
Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
Your binding mode should be
OneWayToSource
notOneWay
,<TextBox Text="{Binding Name, Mode=OneWayToSource}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
Or
TwoWay
if you want your View and the property in your ViewModel to modify each other."As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"