|
In your example above, what is the "cmd" in
<cmd:EventToCommand PassEventArgsToCommand="True"
Thanks
Everything makes sense in someone's mind
|
|
|
|
|
A light MVVM framework called Galasoft
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
http://mvvmlight.codeplex.com/[^]
|
|
|
|
|
Dean Oliver wrote: The whole point of MVVM is not to have code in your code behind.
That's not quite true. The idea in MVVM is not to have code that knows about the model in the code behind of the view. If you want to have code that handles something like a window resize, for instance (it's just an example, and yes you could perfectly easily accomplish this with attached behaviors), it's perfectly acceptable to have this in the code-behind; as long as it doesn't have any model knowledge.
|
|
|
|
|
What's the difference?
Everything makes sense in someone's mind
|
|
|
|
|
"The Silverlight Business Application template creates a project that automatically includes many features that you typically want in a business application, such as controls to log in users and to register new users. The project created by the template is also set up to use Silverlight navigation, which means you can easily add new Silverlight pages for additional functionality.
By default, the Silverlight Business Application template enables authentication, roles, and profiles. The default authentication mode used by the template is Forms Authentication, but you can change the authentication mode to Windows Authentication by simply changing two configuration values. The procedure for making this change is provided here."
Read more about this here[^].
A Silverlight application, in contrast could be a simple web page application.
|
|
|
|
|
Hi
I have a menuItem in my application and I need to open a separate window (inside I have other controls).
I have just used Add from Visual, Add Window ..creates a new xaml and code behinde and I am able to use designer directly.
All I want now is to open this window from
MenuItem_Click
handler function.
Which are my options?
I have tried:
- first to put this window inside a Resource dictionary. It works but I can't use Designer when I open the myDictionary.xaml file.
And I think to declare resources like this is more for templates, styles etc..
<pre><ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window x:Key="pageOptions" Name="Test">
<Grid>
</Grid>
</Window>
</ResourceDictionary>
- I tried also to put the window inside a content presenter. Here I can use Designer on the xaml file but, I cant put a xClass in the window because it needs to be on the root. I cant put it also on the ContentPresenter element. So, I cant use code behind.
<pre><ContentPresenter Name="MyContent">
<ContentPresenter.Content>
<Window>Click Me</Window>
</ContentPresenter.Content>
</ContentPresenter>
|
|
|
|
|
I have found one way by dynamicaly loading XAML with the FileStream and XamlReader.
It has the disatvantage you have to make all your code behind.
Maybe there are other simpler solutions.
|
|
|
|
|
no, first time to hear about MVVM patern. I will start reading when I will have realy complex datas.
|
|
|
|
|
Hi
is that correct to call an element of a view from the viewmodel????
(i'm using MVVMLight)
example:
var grd = (RadGridView)e.Source; (e is KeyEventArgs)
|
|
|
|
|
It is not generally considered good practice for the viewmodel to have any knowledge of the view. generally, when you use the MVVM pattern, you should manipulate the underlying collections, which will be bound to the View in the XAML. You should provide more information as to what you are trying to accomplish, so that someone may help you more specifically.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Thanks for your help,
Can i use <x:code> in xaml for focusing on elements(like gridview) or getting the current item of a gridview or something like that in MVVM????
example:
viewmodel:
void BnbankPreviewKeyDownCommandExecute(KeyEventArgs p)
{
var grd = (RadGridView)p.Source;
var rowIndex = grd.Items.IndexOf(grd.SelectedItem);
if (((Keyboard.Modifiers != ModifierKeys.Shift) & (p.Key == Key.Tab & grd.CurrentColumn.DisplayIndex == grd.Columns.Count - 1)) || (p.Key == Key.Down))
{
if (rowIndex + 1 == grd.Items.ItemCount)
{
if (p.Key == Key.Tab & grd.CurrentColumn.DisplayIndex == grd.Columns.Count -1)
{
p.Handled = true;
InputSimulator.SimulateKeyDown(VirtualKeyCode.DOWN);
}
grd.CurrentCellInfo = new GridViewCellInfo(grd.Items[rowIndex], grd.Columns[0]);
grd.Focus();
grd.CommitEdit();
SaveBnbankCommandExecute(BnbankSelectedRow);
grd.BeginInsert();
}
}
}
view(Xaml):
<telerik:RadGridView x:Name="RadGridView1" ShowInsertRow="True"
ItemsSource="{Binding AllBnbanks}" SelectedItem="{Binding BnbankSelectedRow, Mode=TwoWay}"
CanUserFreezeColumns="False" AutoGenerateColumns="False" Height="300" CanUserInsertRows="True" ShowGroupPanel="False" FontFamily="Tahoma" FontSize="10" Margin="14,2,16,2">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyDown">
<cmd:EventToCommand Command="{Binding Path=BnbankPreviewKeyDownCommand, Mode=Default,UpdateSourceTrigger=PropertyChanged}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataType="{x:Null}" UniqueName="Bnba01"/>
<telerik:GridViewDataColumn DataType="{x:Null}" UniqueName="Bnba02"/>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
|
|
|
|
|
The view should always render and style the data. the viewModel is purely for databound business objects, business rules, commands, and collections of items. Focusing would relate to the gridview so it should be part of the view. Current Item, selected item relates to a collection which is the data being bound by the viewmodel thus that logic should be in the viewmodel.
|
|
|
|
|
sorry to ask again,
I want to be sure if is that correct in MVVM architecture to use x:Code in xaml instead of code behind???????
|
|
|
|
|
At best you should try to avoid writing code in your views code-behind. Remember MVVM is all about rendering data through binding. So you should avoid using x:code. You must to see a view as a template for displaying your data. Not for any business logic. leave xaml for xaml. If you want to add specific logic for your view do it in your viewModel rather.
|
|
|
|
|
I am developing a silverlight application.
Project1 is the webhost i.e. wheer the default.aspx page is
Project2 holds the silverlight pages.
Project3 holds the wcf.
I have set the connectionstring to sql server inside Project3 in the web.config
All works fine.
There is a separate project called DataLayer which I currently use inside my .net windows applications to connect to the database, etc. This project has the connectionstring to sql server saved inside the App.config
Question:
In the Project3, where wcf is used, I try to call the separate project I mentioned above to connect to the database. But the error message is The connection has not been initialized.
Any thoughts on how to solve this please?
|
|
|
|
|
Hi could you please supply the code. where you are directly calling the other project. because somewhere you are not initializing or opening the connection or that connectionstring from the app.config is not in your web.config in project 3. And the only way I can diagnose the problem. Is to see what you are doing else it's all based on assumptions on my part.
thanks
|
|
|
|
|
|
First of all I have a listbox in which I am using a data template to show a stackpanel with a textbox and buttons(one for each day of the week).. The question I have is because these stackpanels are added dynamically to the listbox is there some way that I can know the index of the item in the listbox from which a button is clicked. Using the button click event I don't see a way that I can tell if btnSunday is from row 0 or row 1. Any help would be appreciated thank you.
Humble Programmer
|
|
|
|
|
First of all out the box WPF wraps the listbox control in what is called a CollectionViewSource. This can provide you with information on the currently selectedItem as well as sorting and grouping items.
Get the CurrentItem and on your ObservableCollection of buttons just call IndexOf() and pass in the item. This should give you your index.
for exampe:
In your view xaml on the listbox set
IsSynchronizedWithCurrentItem="True"
then in your viewmodel (just bare inmind my collection is of themes)
public MainViewModel()
{
_theme = new ObservableCollection<Themes>();
_themesView = CollectionViewSource.GetDefaultView(_theme);
_themesView.CurrentChanging += new CurrentChangingEventHandler(_themesView_CurrentChanging);
_themesView.CurrentChanged += new EventHandler(_themesView_CurrentChanged);
}
This allows you to than have access to the current item of the collection.
In the event below you can do whatever you want with the current object. eg; index of your button item that you have selected.
void _themesView_CurrentChanged(object sender, EventArgs e)
{
ListCollectionView view = sender as ListCollectionView;
int index = _theme.IndexOf(view.CurrentItem as Themes);
}
Hope this helps to some extent.
|
|
|
|
|
I have a user control whose data context is a ViewModel. The user control has a listbox bound to an ObservableCollection.
When I have to add a new object to the listbox, I click on the 'Add new' button that opens an Open-file-dialog. After selecting the file from this dialog, I want to do further operations in the ViewModel. What is the best way to call/refer a method of the ViewModel in the user-control code-behind? Please suggest a solution.
|
|
|
|
|
2, no 3, no, actually there are a number of ways to achieve this. Some, Galasofts MVVM Light use a static copy of the Viewmodel, there are also a controller type pattern and you can always pass an instance of the viewmodel around. And I'm confidant there are more solutions than that.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Could you please tell us which part of this scenario requires you to refer to the VM in the View? I can see nothing in there that indicates that you need to do this.
|
|
|
|
|
The code-behind of the user-control is handling the 'Add New' button click event because I wanted to work with the Open file dialog (and I thought I could not have openfiledialog in the ViewModel, but now it seems like I can do it with Relaycommand).
After selecting the file from the Open file dialog, I want the further operations to be done in the ViewModel, so here is where I need to refer to the VM from the view.
|
|
|
|
|
The standard way for doing this in an MVVM type application is to have an interface that copes with the OpenFileDialog, and use this.
What I mean by this, is that you have an interface (we'll call it IFileOpenDialog) and that you create a class that implements this interface. Now, this class would simply wrap the call to opening the file dialog, and you would register it with something like a service locator - then your VM retrieves an instance of this class from the service locator using the interface and uses that to open the dialog.
Typically, your command would look something like this:
private void OpenFile(object o)
{
IFileOpenDialog fileOpen = ServiceLocator.Get<IFileOpenDialog>();
fileOpen.Filters = "All files (*.*)|*.*|";
bool? result = fileOpen.ShowDialog();
if (result.HasValue && result.Value == true)
{
myFile = fileOpen.FileName;
}
} I've just knocked this up in the CP editor, so I apologise for any syntax errors, but hopefully this should demonstrate what I've been talking about.
|
|
|
|
|
Pete O'Hanlon wrote: I've just knocked this up in the CP editor
I think that answer requires a pregnant pause before continuing...
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|