Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. MVVM architecture

MVVM architecture

Scheduled Pinned Locked Moved WPF
architecturewpftutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mi_n
    wrote on last edited by
    #1

    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)

    W L 2 Replies Last reply
    0
    • M mi_n

      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)

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M mi_n

        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)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        0
        • L Lost User

          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.

          M Offline
          M Offline
          mi_n
          wrote on last edited by
          #4

          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"

          L D 2 Replies Last reply
          0
          • M mi_n

            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"

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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 as SelectedItem.

            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.

            1 Reply Last reply
            0
            • M mi_n

              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"

              D Offline
              D Offline
              Dean Oliver
              wrote on last edited by
              #6

              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.

              M 1 Reply Last reply
              0
              • D Dean Oliver

                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.

                M Offline
                M Offline
                mi_n
                wrote on last edited by
                #7

                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???????

                D 1 Reply Last reply
                0
                • M mi_n

                  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???????

                  D Offline
                  D Offline
                  Dean Oliver
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups