Question

I have a Silverlight treeview that is dynamically populated with treeviewitems(representing views). I have things working fine but when I click on an treeviewitem say, View 2.01, I want to be able to get the ItemId(for View 2.01) and have the View 2.01 shown in the ViewSwitchingRegion or simply view switch by ItemId. Here’s what I have done. What am I missing?

<UserControl.Resources>
            <sdk:HierarchicalDataTemplate x:Key="childNodeDataTemplate" 
                                              ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Path=Header}"/>
            </sdk:HierarchicalDataTemplate>
        </UserControl.Resources>

        <sdk:TreeView Name="DataTreeView"
                             ItemsSource="{Binding DataItems}"
                             ItemTemplate="{StaticResource childNodeDataTemplate}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectedItemChanged">
                    <i:InvokeCommandAction Command="{Binding Path=ShowViewCommand}"
                                           CommandParameter="{Binding ElementName=DataTreeView}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="MouseDoubleClick">
                    <i:InvokeCommandAction Command="{Binding Path=ShowViewCommand}"
                                           CommandParameter="{Binding ElementName=DataTreeView}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </sdk:TreeView>
    </UserControl>

    public class DataViewModel : ViewModelBase
    {
        #region Fields

        private readonly IRegionManager _regionManager;
        ...

        #endregion

        #region .Ctors

        public DataViewModel()
        {
            //IRegionManager regionManager, ...

            _regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

            //SomeMethodToLoadDataItems();
        }

        #endregion

        #region Methods

        ObservableCollection<DataItem> _dataItems;
        public ObservableCollection<DataItem> DataItems
        {
            get { return _dataItems; }
            set
            {
                if (_dataItems != value)
                {
                    _dataItems = value;

                    RaisePropertyChanged("DataItems");
                }
            }
        }

        RelayCommand _showViewCommand;
        public ICommand ShowViewCommand
        {
            get
            {
                if (_showViewCommand == null)
                {
                    _showViewCommand = new RelayCommand
                    (
                        p => ShowView(p)
                    );
                }
                return _showViewCommand;
            }
        }

        private void ShowView(object param)
        {
            if (param != null && param == ItemId) //ItemId for View 2.01
            {
                _regionManager.RequestNavigate("ViewSwitchingRegion", new Uri("View 2.01", UriKind.Relative));
            }
        }
    }

    public class DataItem : ViewModelBase
    {
        public DataItem()
        {
            this.Children = new ObservableCollection<DataItem>();
        }

        private string header;
        public string Header
        {
            get {return this.header;}
            set
            {
                if (this.header != value)
                {
                    this.header = value;
                    OnPropertyChanged("Header");
                }
            }
        }

        private DataItem _itemId;
        public DataItem ItemId
        {
            get { return _itemId; }
            set 
            {
                if (value != _itemId)
                {
                    _itemId = value;
                    RaisePropertyChanged("ItemId"); 
                }
            }
        }
        public ObservableCollection<DataItem> Children { get; set; }    
        }
    }
Was it helpful?

Solution

I finally got around it. I changed the object param to TreeView param as below

`

private void ShowView(TreeView param)
{
    if (param.SelectedItem != null) //ItemId for View 2.01
    {
        var itemId = ((DataItem)param.SelectedItem).ItemId;
        //should be able to manipulate the rest of the bits with the itemId
    }
}

Make changes to the ShowViewCommand such that the ShowView param is cast to TreeView
    Public ICommand ShowViewCommand
    {
         get
             {
                 if (_showViewCommand == null)
                 {
                     _showViewCommand = new RelayCommand
                     (
                         p => ShowView(p as TreeView)
                     );
                 }
                 return _showViewCommand;
            }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top