|
But in reality the property shouldn't be called "ShouldShow" but "IsOverseasTransaction" (or whatever business rule the View is using to determine visibility.
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|
|
Hi, I have just started working on MVVM, MVVM is new for me, this stackpanel visibility i have to show/hide on button command. Can you please provide any idea how to show/hide on button command from view model.
Thanks Pete O'Hanlon.
|
|
|
|
|
I already have. All the pieces are there. You hook up a command - you set the boolean property and then you raise the PropertyChanged event. As long as you have this view model hooked up to your DataContext, and you use the XAML I showed, you should be good to go.
|
|
|
|
|
Thanks Pete O'Hanlon, its working perfectly.
|
|
|
|
|
Hi,
How to send the content of a button from view to ViewModel on click command.
|
|
|
|
|
DO you have some code you have tried?
|
|
|
|
|
Thanks for reply. No sorry i dont have any code right now, i have only button click command code.
can you give any solution.
|
|
|
|
|
You should try and code something up and then ask why it isnt working. We can tell you what your problems are, but youll have to do the work yourself, deal?
|
|
|
|
|
When you bind the command add a CommandParameter, this is picked up as an object in the VM method you bind to.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
yes its working, thanks Mycroft Holmes.... 
|
|
|
|
|
This sounds incorrect. Can you show some code?
You would want to pass the context of the view to the view model.
|
|
|
|
|
Abhinav, I got the solutions, thanks for showing interest.
you asked for code, so i am displaying here:
Text = {Binding Command} CommandParameter="{Binding ElementName=btn1, Path=Content}"
|
|
|
|
|
I am trying to show a chart control in my application where the chart value comes at runtime. To imitate that I tried adding a chart control and populate value on button click, but every time I try to do so error: "No suitable axis is available for plotting the dependent value"
My XAML looks like this:
<chartingToolkit:Chart Name="chart1" Height="603" VerticalAlignment="Top">
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Orientation="Y" Interval="" ></chartingToolkit:LinearAxis>
</chartingToolkit:Chart.Axes>
<chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" />
</chartingToolkit:Chart>
in code behind I have created following class to hold data:
public class ChartData
{
public ChartData(string key, double val)
{
Key = key;
Value = val;
}
public string Key;
public double Value;
}
Also I created an ObservableCollection of this class. Now on every button click I add certain data to this collection and at the load method of page I set the datacontext of chart to this observable collection.
I tried a lot but the error won't go.
Any help is appreciated.
Thanks in advace.
|
|
|
|
|
|
I am using WPFToolkit's chart control. However I figured out the issue. The class to which my collection was binded was having private properties istead of public. On changing this it's working fine.
Thanks for replying 
|
|
|
|
|
hi,
is there a way to get the closed now !!!!!!, expression gallaery website content,
mirrors, archives, backup , .... anything !!
thanks and good day.. or night
|
|
|
|
|
Have you tried the links on the closure announcement[^]?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am using Date and Time picker from windows phone toolkit. i have both controls on my page. I am using two way binding to the System.DateTime property in one of my class, both the controls i.e. the date picker and time picker are bound to one property. everything is working fine except a corner case (which is not really a corner case)
When i change the value in date picker, the property in my class is updated, which is good. When i change the time through time picker the Time is updated in my DateTime property. However while changing the time if i go past 00:00hrs (i.e. 'scroll' through hours until i go pass 0) the date value of the DateTime property is set to current date. This is something i want to avoid, Is there a way i can solve this problem ?
<toolkit:DatePicker Value="{Binding MyDateTime, Mode= TwoWay}"/>
<toolkit:TimePicker Value="{Binding MyDateTime, Mode= TwoWay}"/>
public DateTime MyDateTime
{
get { return dateTime; }
set { dateTime = value; }
}
|
|
|
|
|
Not likely, each one quite reasonably believes it has full authority to set the entire value for the bound property.
You'll have to bind to two properties and then combine the Date and Time parts into a single value for your later use.
|
|
|
|
|
yes, that seems to be the only solution. i have now used to properties and i am combining the date and time
|
|
|
|
|
I have a MaterialsViewModel. There are 7 different types of materials. I am defining the views->VM's in my resource file like this:
<DataTemplate DataType="{x:Type vm:MaterialsViewModel}">
<vw:MaterialsView/>
</DataTemplate>
In the VM I set an enum called 'MaterialType' based on the type of material.
How do I set up the resource file to select the right view?
Thanks
If it's not broken, fix it until it is
modified 17-Oct-13 16:15pm.
|
|
|
|
|
You are doing it bass akwards IMO
Remember, one of the mantras of MVVM is that the VM knows nothing about the view. The view obviously knows about the VM its rendering. If you setting up DataTemplates as you have, you are implying that you are creating a VM first and then assigning it a view based on the data type. This is implying that in some VM, you are newing up other random VMs (BAD -- VMs should not know about each other) and making "random decisions" for the VM / View relationship (also BAD).
What makes more sense (to me at least lol) is that as a command handler, I want to load a particular view. That view should then know what VM it needs and new it up and wire everything up. As I said in the paragraph above, if I am newing up VMs in code, I have to also new up the window, set the DataContext, etc. This is too much intimate knowledge.
What I like to do is just say "I want this particular view to popup":
Window logonWindow = LoadComponent(new Uri("Views/LogonWindow.xaml", UriKind.RelativeOrAbsolute)) as Window;
logonWindow.ShowDialog(); // or .Show() or whatever...
using ViewLocator & DI, Views/LogonWindow.xaml will automatically new up the appropriate VM, set the DataContext, call InitializeComponent(), etc.
In answer to your other question... if you have different look & feel, you might break out the two look & feels into two different templates then have a tiny "loader template"... the loader template would just use a DataTrigger to swap out to the other template if needed.
So your loader template will by default apply the PC Template and if your global/static IsTablet DP == true, it will apply the Tablet Template instead.
This is how custom controls are generally written. You have two styles for example: an XP style and an Aero style. Apply the XP style by default and then swap out for the Aero template if IsAero == true.
POH... if you are reading this... wouldn't mind hearing your comments on this approach. To me, this makes more sense then going the other way...
|
|
|
|
|
Where the first approach really makes sense is if you have a HierarchicalDataTemplate that you are applying to a deep hierarchy where a top level VM is composed up of multiple different child VM's. This is a great way to separate out the look and allows you to easily build a complex hierarchy, allowing WPF to do its thing.
However, I have to agree that 99% of the time, I would go with the approach you recommend. Indeed, to complement this approach, I'd highly recommend that Kevin needs to investigate MefedMVVM[^].
|
|
|
|
|
Interesting - and pls. excuse me from hijacking the thread.
I'd always thought differently;
Use does something on a view (clicks a button, selects a row on a gird, whatever)
The ViewModel corresponding to the view has a command executed (e.g. UserSelectedACustomerCommand)
ViewModel now knows that, as a result of that command, the ShowCustomerViewModel needs to be shown
Whether through DI, hard coding, via a Controller or magic, the VM instntiates a ShowCustomerViewModel and tells it it needs to 'do its stuff'
'Doing its stuff' involves (again, 'somehow', the tech isn't too important) instantiating the View and setting its DataContext.
The idea being that except for the actual button click (or whatever) everything else is VM-based - because we're modelling the view.
If I understand you above post correctly, you are saying that the click on teh View is telling the View to show the CustomerDetailView - but isn't it better to keep views entirely separate from one another, as the VM is a model - the View is just the eye candy?
Am I missing something, misreading your post or just plain wrong?
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|
|
_Maxxx_ wrote: ViewModel now knows that, as a result of that command, the ShowCustomerViewModel
needs to be shown Whether through DI, hard coding, via a Controller or magic,
the VM instntiates a ShowCustomerViewModel and tells it it needs to 'do its
stuff'
That makes no sense to me lol. VMs are not shown, Views are.
_Maxxx_ wrote: If I understand you above post correctly, you are saying that the click on teh
View is telling the View to show the CustomerDetailView - but isn't it better to
keep views entirely separate from one another, as the VM is a model - the View
is just the eye candy?
Slight misread. The view doesn't do anything. It doesn't have any code behind. In fact, I always delete the .cs code behind file just so some douche won't come around and stick stuff in there . Clicking on a button in the view executes a command in the VM. The VM knows it needs to show a dialog or window or slide something into view via an animation, or whatever. Doing it the way you suggested means VM A knows about VM B and intimately knows VM B wants View B attached to it, the DataContext set, etc.
Going the way I suggested is more consistent with how the web works. I don't do web stuff, so its kind of ironic... but on the web, you redirect to the .html (the "view"), not the .cs (the "view model").
|
|
|
|