MVVM architecture
-
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
-
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)
Of course you can if you have been provided a reference (or a reference that can get you a reference as you have written). However, as is pointed out it is bad practice. Your VM should be designed such that it does not need to access the view. The point of MVVM is (well there are other reasons as well), a seperation of concerns. By directly coupling the view in the view model you are beginning to bind their concerns.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
Of course you can if you have been provided a reference (or a reference that can get you a reference as you have written). However, as is pointed out it is bad practice. Your VM should be designed such that it does not need to access the view. The point of MVVM is (well there are other reasons as well), a seperation of concerns. By directly coupling the view in the view model you are beginning to bind their concerns.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
Thanks for your help, Can i use 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" -
Thanks for your help, Can i use 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"I am not clear as to what you are asking. If something is changing the styling or look it belongs in the view. Wether you do it in pure XAML or use the code behind is up to you (I try to do it all in XAML). Anything that modifies the business objects should be in the view model. Your VM should NOT be setting focus, as that is view code. It can however set the
ActiveItem
or more commonly known asSelectedItem
.Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
Thanks for your help, Can i use 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"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.
-
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.