Execute an event before another one with MVVM

4 posts, 0 answers
  1. Chris Thierry
    Chris Thierry avatar
    138 posts
    Member since:
    Jun 2010

    Posted 13 May 2011 Link to this post


    Hi, I'm new using MVVM, I need to know How can I execute an event before another one in Silverlight using a RadGridView, for example I have a RadGridView and a TextBox, when I enter something is the textbox and then click in a row, I want to show a message with the text and the id of the selected row.
    It just a test, is working by the way but! the problem is when I enter something in my textbox and right after that I click in my RadGridView, is executing the SelectionChanged of the grid before executing the lostfocus of the textbox

    This is my xaml:
    <Grid x:Name="LayoutRoot">
              
            <ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}">
      
                <StackPanel x:Name="ContentStackPanel" Style="{StaticResource ContentStackPanelStyle}">
      
                    <telerik:RadGridView 
                        x:Name="rgvSample"
                        IsReadOnly="True"
                        ItemsSource="{Binding LoadRadGridView, Mode=OneWay}"  
                        AutoGenerateColumns="True">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectionChanged">
                                <i:InvokeCommandAction 
                                        Command="{Binding RowIsActiveCommand}" 
                                        CommandParameter="{Binding ElementName=rgvSample, Mode=OneWay}" />
      
                            </i:EventTrigger>
                       </i:Interaction.Triggers>
      
                    </telerik:RadGridView>
      
                    <!--<TextBox Grid.Column="1" Grid.Row="1" x:Name="myTextBox" Height="25" Text="{Binding AddedText, Mode=TwoWay}" />-->
                    <TextBox Grid.Column="1" Grid.Row="1" x:Name="myTextBox" Height="25" >
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="LostFocus">
                                <i:InvokeCommandAction
                                    Command="{Binding TextLostFocusCommand}"
                                    CommandParameter="{Binding ElementName=myTextBox, Path=Text, Mode=TwoWay}"
                                </i:InvokeCommandAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </TextBox>
                      
                      
                </StackPanel>
      
            </ScrollViewer>
      </Grid>

    and this is my code:

    public class RowClickViewModel : ViewModelBase
        {
            private List<Item> _itemList;
            public List<Item> LoadRadGridView
            {
                get { return _itemList; }
                set { _itemList = value; }
            }
      
            private string _addedtext;
            public string AddedText
            {
                get { return _addedtext; }
                set
                {
                    if (_addedtext != value)
                    {
                        _addedtext = value;
                        OnPropertyChanged("AddedText");
                    }
                }
            }
              
            public RowClickViewModel()
            {
                LoadRadGridView = GetItems();
            }
      
            public ICommand RowIsActiveCommand
            {
                get
                {
                    return new DelegateCommand(BeginRowIsActive, (o) => true);
                }
            }
      
            public ICommand TextLostFocusCommand
            {
                get
                {
                    return new DelegateCommand(BeginLostFocus, (o) => true);
                }
            }
      
            public void BeginLostFocus(object param)
            {
                AddedText = param.ToString();
            }
      
            public void BeginRowIsActive(object param)
            {
                Telerik.Windows.Controls.RadGridView myGridView = (Telerik.Windows.Controls.RadGridView)param;
                  
                string caption = "Message";
                MessageBoxButton buttons = MessageBoxButton.OK;
                string message;
                  
                message = string.Format("{0}\n{1}", ((Item)(myGridView.SelectedItem)).Name , AddedText); 
                MessageBox.Show(message, caption, buttons);
            }
      
            public List<Item> GetItems()
            {
                return new List<Item>
                {
                    new Item { Id = 1, Name = "Item 1" },
                    new Item { Id = 2, Name = "Item 2" },
                    new Item { Id = 3, Name = "Item 3" },
                    new Item { Id = 4, Name = "Item 4" },
                    new Item { Id = 5, Name = "Item 5" },
                    new Item { Id = 6, Name = "Item 6" },
                    new Item { Id = 7, Name = "Item 7" },
                    new Item { Id = 8, Name = "Item 8" },
                    new Item { Id = 9, Name = "Item 9" },
                    new Item { Id = 10, Name = "Item 10" }
                };
            }
      
            public class Item
            {
                public int Id { get; set; }
                public string Name { get; set; }
            }
        }

    Maybe I'm using the wrong event?
    Any help please!!
    Thank you.
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 16 May 2011 Link to this post

    Hi Chris Thierry,

    Generally, you cannot change the order of the events, some of them might be cancelable, but still their time of execution is predefined. Consequently, you may have to change the order of the implemented logic.
    On a side note, may I suggest some improvements on the code provided ? Firstly, defining the RadGridView in a control which measures with infinity will cause the grid's virtualization to be turned off. Such control is StackPanel. Removing the virtualization might cause performance degrade if you handle a lot of data. Secondly, defining visual elements in the ViewModel is not recommended. Why do you need a RadGridView in it ? Basically, the idea of the ViewModel is to separate the view - the UI elements - from the data. So, you may define the Item class in a new class definition, not inside the ViewModel.
    Let me know in case you need any further assistance.
     

    Greetings,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Chris Thierry
    Chris Thierry avatar
    138 posts
    Member since:
    Jun 2010

    Posted 16 May 2011 Link to this post

    Hi,

    thank you for the answer, I'm just doing a test, is not a real project, I was just playing with an example.
    When you said "you may have to change the order of the implemented logic", could you give me an example of this?

    Thank you for the advice! 
  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 16 May 2011 Link to this post

    Hello Chris Thierry,

    What I meant with changing the logic was to perform the actions you require in the event that will be fired when you want it. So, as it is in the example, you may pop up a message during the SelectionChanged event or SelectionChanging. 
     

    All the best,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top