开发者

Using UI-related types in ViewModel and TabControl in silverlight

开发者 https://www.devze.com 2023-04-12 19:22 出处:网络
In my Silverlight app I have a view containing a tab control and a view model of this view. When the selected tab is changed, I need to refresh its data. In order to do that in the view model I\'m usi

In my Silverlight app I have a view containing a tab control and a view model of this view. When the selected tab is changed, I need to refresh its data. In order to do that in the view model I'm using a command triggered by EventTrigger in the view and passing 开发者_如何学运维the appropriate event args to it (as described here http://weblogs.asp.net/alexeyzakharov/archive/2010/03/24/silverlight-commands-hacks-passing-eventargs-as-commandparameter-to-delegatecommand-triggered-by-eventtrigger.aspx). Each tab item has its own view model and, therefore, to distinguish which view model I have to use to refresh the data, I'm watching the header in the tab item which I can get from the event args, e.g:

_tabSelectionChangedCommand = new DelegateCommand<SelectionChangedEventArgs>(TabSelectionChanged);
public ICommand TabSelectionChangedCommand
{
   get { return _tabSelectionChangedCommand; }
}

private void TabSelectionChanged(SelectionChangedEventArgs e)
{    
    var tabItem = (TabItem)e.e.AddedItems[0];
    if (tabItem.Header == "Header1" )
    {                    
         TabItem1ViewModel.Refresh();
    }
     .....
 }

So, my question is : Is good that I'm using in the view model the types related to the UI(TabItem, SelectionChangedEventArgs) and are there better ways to do what I've described above?


Maybe you can bind the SelectedIndex of the TabControl to a property defined in your viewmodel and attach an InvokeActionCommand to the TabControl and subscrible to its SelectionChanged event.

Then when the command gets called, check which index it is then load the data accordingly?

0

精彩评论

暂无评论...
验证码 换一张
取 消