|
<StackPanel>
<TextBox Text="{Binding MyViewModelList[0]}">
<TextBox Text="{Binding MyViewModelList[1]}">
<TextBox Text="{Binding MyViewModelList[2]}">
</StackPanel>
I'm trying to solve using this code, but is always null.
|
|
|
|
|
Have You set the DataContext to the ViewModel?
David
|
|
|
|
|
Yes David I have set the DataContext. Can you look on above code, is anything missing or else if you have any way then please share with me.
Thanks
|
|
|
|
|
Without knowing what is in your ViewModel, That you have not shown. It would be hard for me to guess.
What you should use is a DataTemplate to display each member of your list.
David
|
|
|
|
|
private List<string> _myViewModelList;
public List<string> MyViewModelList
{
get
{
return _myViewModelList;
}
set
{
if (_myViewModelList != value)
{
_myViewModelList = value;
RaisePropertyChanged("MyViewModelList");
}
}
}
private string MouseDown()
{
string finalString = "";
foreach (string str in MyViewModelList)
{
finalString += str +", ";
}
return finalString;
}
David, can you check it where i am doing mistakes. List has null value when i use mouse down event.
Thanks.
|
|
|
|
|
Where do you set the value of the MyViewModelList ?
The bindings that you showed were reading from the MyViewModelList .
The XAML binding will not create the list for you.
Also to note: adding to, or removing from the MyViewModelList will not cause the RaisePropertyChanged to be raised. As you have this coded, that is raised only when the MyViewModelList property is assigned to (replaced).
|
|
|
|
|
Matt T Heffron can you share any code that will send all textboxes content to the viewmodel list/observablecollection.
Thanks
|
|
|
|
|
First you need to initialize MyViewModelList when the View Model is constructed.
private const int NumberOfStrings = 3;
public ViewModel()
{
MyViewModelList = new List<string>(NumberOfStrings);
for (int i = 0; i < NumberOfStrings; ++i)
{
MyViewModelList[i] = string.Empty;
}
}
public List<string> MyViewModelList { get; private set; }
At this point, the TextBoxes will be bound to the empty strings, and should update the corresponding entry in the MyViewModelList whenever they lose focus.
|
|
|
|
|
Thank you very much Matt T Heffron.
|
|
|
|
|
When I select the row and click on edit button:
-Display one stack panel
-Stack panel has textboxes
-Row items should display in textboxes
|
|
|
|
|
Bind your datagrids ItemsSource to the collection, bind the SelectedItem to an a single instance property of that collection.
Create a container for the text boxes, probably a grid, bind each text boxes text property to the SelectedItem.FieldName
Now go and find some tutorials and work through them because you cannot get enough information from forum posts to learn.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Thanks Mycroft, it worked...
|
|
|
|
|
Please let me know how to change the content of button from ViewModel
|
|
|
|
|
If the content is just simple text, and you have bound it to the property, simply raise the PropertyChanged event on this property once you have updated it's text.
|
|
|
|
|
Text="{Binding Content}" with the proper Content should work for you.
Text is a dependency property and you can bind a property to it.
|
|
|
|
|
How to take mouse down event in ViewModel.
|
|
|
|
|
You don't. That's not what you use a ViewModel for. What you would normally do is create a command in your ViewModel and then hook it up to an event in your XAML using something like Laurent Bugnion's EventToCommand[^].
|
|
|
|
|
Use a command or an attached behaviour to handle mouse down.
As a last resort, you can call the viewmodel mouse down method from the view's click event.
However, this is not an MVVM way to handle events in xaml based programming.
|
|
|
|
|
Do you have any code for the same, can you please share it..
|
|
|
|
|
|
If you don't want to complicate things you can always just respond to the MouseDown event in your view's code behind. Here you have access to your ViewModel (via the DataContext) so can 'tell' your ViewModel to perform some action (whether via a command or a property or method - whatever works for you)
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|
|
That is the way we set it up, that and navigate being the only code behind. Still it seems to break the fanatical MVVM design.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: fanatical
Le mot juste!
When I have queried such fanatics, the best real reason I can get out of them is that it stops 'bad programmers' putting non UI code in the View
I am insulted by this, personally. I don't do bad code (that often)
I'm all for maintainability, and two or three lines of C# (event handler itself and reference to the VM) IMHO is far more predictable, legible and maintainable than screeds of 'clever' code.
[edit]
Just noticed the response below - which looks elegant but surely including a library in order to do something that can easily be done in .cs code behind is somewhat OTT also.
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|
|
It seems the OP doesn't actually read anything that is linked to outside this site. I had linked to a complete working example above, and still the OP wanted someone to write his code.
|
|
|
|
|
You could use interaction triggers;
<Window x:Class="MainWindow"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions">
...
<Button Name="DeleteButton" Content="Delete" Height="25" Width="60">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<ei:CallMethodAction MethodName="Delete" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
...
In the ViewModel;
public class MainWindowViewModel
{
public void Delete()
{
}
}
"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"
|
|
|
|