Can I quickly confirm if this is a best practice or I should be rethinking my process with MVVM. (I am new to this)
<ComboBox DataContext="{Binding MemberMain, Source={StaticResource Locator}}" ItemsSource="{Binding PayMethodList}" Text="{Binding DataContext.Member.MM_PaymentMethod, ElementName=TabItemClient}" Margin="0,0,15,0"/>
I am binding a combobox itemssource to one view model and the text to another.
Thanks in advance Scott
The only thing glaring that I see here is the DataContext binding. Do you really need this?
Other than that. If it works and remains concise, if you can swap bits in/out with little issue, then it is fine.
Posting your view model may help further assessment.
Related
I have to sort the tabs within IconTabBar control alphabetically. I have searched for the sorting functions in controller but those are triggered after an UI event.
whereas i need to make those sorting whenever the second view loads(or when we travel to second view).
Can anyone please suggest how to achieve this in view itslef( Note:- i'm using XML view).below is the view code for the same.
<IconTabBar id="idIcontabs" items='{ProModel>/results}'
showSelection="true" stretchContentHeight="true" class='styleForIconTab'>
<items>
<IconTabFilter text="{ProModel>Grpname}">
Thanks a lot in advance..!!!
Regards,
Ranjan R
Yeah it was tabular data i wanted to sort i achieved it using "sorter" for the entity in the model. and it rendered me the view with the data in sorted format.
Thanks all who provided their time to answer this question.
I have some small bits of HTML that are used in nearly all of my views. I would like to be able to abstract these out of the views so that when I make changes to one view I don't have to update it in everyone of my views. I am not sure how to achieve this. I have tried the following via the MVVM declarative syntax.
<div id="toolbar" class="pull-right" data-role="view" data-template="edit-tool-bar"> </div>
But this doesn't do anything. I do not want to add code to push the HTML into the view if I can avoid this, that is the entire point of MVVM, right? I would be very thankful for any help.
The standard seems to be to use the source binding and specify a template. Since the main view is already bound to a VM telling I set the source binding to this.
<div id="toolbar" class="pull-right" data-bind="source: this" data-template="edit-tool-bar"></div>
I am new to MVVM and need help on below scenario.
I have a Stack panel added on my view, now I have to add few controls dynamically to this stack panel through viewmodel. For this I need a handle of stack panel in my viewmodel. Can anybody please guide me how I can access stack panel in my viewmodel.
I read in other bloges that it can be done by using Dependency property. but still I am not able to find way to solve this issue.
Couple of things to note first. The intention of the ViewModel in the MVVM pattern is to provide separation from the View. Therefore, your ViewModel should have no knowledge of the View itself nor the controls contained in the View. Secondly, what you should be attempting to do is have your View bind to a property of your ViewModel (with the understanding that your ViewModel serves as the DataContext of your View). Normally, you would bind a control's ItemsSource property to some collection in the ViewModel. However, you will notice the StackPanel does not implement the ItemsSource dependency property. Instead, use ItemsControl in place of your StackPanel. I would suggest some additional reading on the MVVM pattern and the binding mechanics for additional clarification.
Totally agreed with Backlash,
It seems that your ViewModel and your View are too much coupled;
There are a bunch of resources on the Internet, but here are some I preferred:
Jason Dolinger presentation video on MVVM
John Smith article introducing MVVM
I have done this before with a user control. I've had a collection of objects that i needed to dynamically to controls in a StackPanel. You can also do it with any control...I'll use a TextBlock in this example.
Create a data template with the control you want wrapped in a stack panel:
(MyText is a property in the object of the collection... see below)
<DataTemplate x:Key="MyTemplate">
<Grid Margin="0">
<StackPanel>
<TextBlock Text="{Binding Path=MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
Then the key is to bind to a collection of objects using an ItemsControl:
(The collection is on your viewModel)
<ItemsControl
ItemsSource="{Binding ACollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate=" {StaticResource MyTemplate}" Background="Transparent">
</ItemsControl>
So for example if there are 3 items in "ACollection" there will be 3 TextBlocks stacked on top of each other if there are 5 in the collection there will be 5 TextBlocks etc.
Thanks All for your help, Here I solved the problem
In view model I have created ObservableCollection of FrameWorkElement type, which can hold any other controls which will be decided at runtime. Control can be a text box or button.
Public ObservableCollection < FrameWorkElement > Test
{
get{....} set{...}
}
Now I can add/set any other control to Test
Tets.Add(new TextBox()); Or Button , this will be decided at runtime.
Now bind this "Test" to ItemsControl.
<ItemsControl x:Name="itemsControl" ItemsSource={Biding Test}>
</ItemsControl>
Can anyone tell me what the actual syntax is for EventToCommand class. From what I believe is that EventToCommand class works with Silverlight / WPF and WP7, hence I think its a better choice to go down.
From what I believe, I can add any click event and get it forced into my ViewModel, but I am having an issue in finding the best way to do this.
I know you can add it without Blend, but are there snippets available?
Or is there an easier way to add it via VS 2010? Any help or if anyone knows of a good tutorial on this would be great.
Suppose you use .NetFramework4:
First add namespace:
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
Syntax for the Loaded event.
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding Mode=OneWay, Path=LoadedCommand}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
I updated my project and it looks like they moved the command to:
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
0) if you dont't know WPF and MVVM, then read Josh Smith article about WPF and MVVM pattern https://msdn.microsoft.com/en-us/magazine/dd419663.aspx
1) In your project add package (through NuGet) MvvmLightLibs
2) add reference to System.Windows.Interactivity
3) In "View" XAML add:
a)
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
b)
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<command:EventToCommand Command="{Binding OnClosingCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Window>
4) In ViewModel add necessary property
public ICommand OnClosingCommand
{
get
{
return new RelayCommand(() => SomeMethod());
}
}
P.S. In your View should be specified DataContext (XAML)
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
It is work. I myself just learned.
I am trying to find a good way to have a dropdownlist by specifying UiHint "DropDown" to the property of the ViewModel and then just using HtmlHelper EditorFor to render the dropdown via an generic EditorTemplate so that it can be used across solutions. I found a very good approach by Tom Schreck Here It works fine. The only thing from keeping it from being perfect is that the dropdownlist is not part of the validation. If I would like to have a "choose" option (added manually in the EditorTemplate) as default and no value is chosen it results in an error. I believe it's because the DropDownList is not created from the DropDownListFor - Helper? I know that Tom wrote that he tried that one, but is there no way of adding items to the SelectList in the loop with the custom values? I cannot do it since I'm a beginner but I think it would be really great if the dropdown could be inlcuded in the validation. This way you could populate the selectList in the controller and keep the same format as the other properties. Sorry for my vocabulary, as I said, I'm a beginner. But I hope you understand what I mean.
I would really appreciate if someone helped me to solve this as I feel I have tried everything..
Regards max