如何通过Id获取Silverlight TreeViewItem?

时间:2013-12-29 10:18:35

标签: silverlight mvvm prism-4

我有一个Silverlight树视图,它使用treeviewitems(代表视图)动态填充。我的工作正常但是当我点击treeviewitem说,View 2.01时,我希望能够获得ItemId(对于View 2.01)并在ViewSwitchingRegion中显示View 2.01或者只是按ItemId查看切换。 这就是我所做的。我错过了什么?

<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; }    
        }
    }

0 个答案:

没有答案