在手动检查可视元素之前绑定到ItemsSource不起作用(MVVM)

时间:2010-09-02 11:56:40

标签: wpf xaml mvvm binding

我有Xaml,基本上应该将选定选项卡的viewmodel的一组ContextualButtons绑定到ToolBar的ItemsSource属性。出于某种原因,除非我使用Snoop手动检查元素,否则这种绑定实际上并不存在......似乎窥探元素的行为在某种程度上以某种方式重新查明绑定。

有谁知道我在这里做错了什么?如果我也使用Listbox,这种行为是一样的,所以我知道这是我做错的事情......但我不确定是什么。

SelectedView是Xam Tab控件中所选视图的绑定属性。

XAML

<ToolBar DataContext="{Binding SelectedView.ViewModel}"
     ItemsSource="{Binding ContextualButtons}" >     
     <ToolBar.ItemTemplate>
      <DataTemplate>
<!--       <Button ToolTip="{Binding Name}"-->
<!--         Command="{Binding Command}">-->
<!--        <Button.Content>-->
<!--         <Image Width="32" Height="32" Source="{Binding ImageSource}"/>-->
<!--        </Button.Content>-->
<!--       </Button>-->
       <Button Content="{Binding Name}"/>
      </DataTemplate>
     </ToolBar.ItemTemplate>

    </ToolBar>

代码

      public class TestViewModel : BaseViewModel, IBulkToolViewModel
      {


        public TestViewModel()
        {
          ContextualButtons = new ObservableCollection<IContextualButton>()
                                {
                                  new ContextualButton("Test Button",
                                                       new DelegateCommand<object>(
                                                         o_ => Trace.WriteLine("Called Test Button")), String.Empty)
                                };
        }

        public string Key { get; set; }
        private ObservableCollection<IContextualButton> _contextualButtons;
        public ObservableCollection<IContextualButton> ContextualButtons
        {
          get { return _contextualButtons; }
          set
          {
            if (_contextualButtons == value) return;
            _contextualButtons = value;
            //OnPropertyChanged("ContextualButtons");
          }
        }

      }

      public partial class TestView : UserControl, IBulkToolView
      {
        public TestView()
        {
          InitializeComponent();

        }


        public IBulkToolViewModel ViewModel { get; set; }

      }
      public class ContextualButton : IContextualButton
      {
        private string _name;
        public string Name
        {
          get { return _name; }
          set { _name = value; }
        }

        public ICommand Command { get; set; }
        public string ImageSource { get; set; }

        public ContextualButton(string name_, ICommand command_, string imageSource_)
        {
          Name = name_;
          Command = command_;
          ImageSource = imageSource_;
        }
      }

  public class BulkToolShellViewModel : BaseViewModel, IBaseToolShellViewModel, IViewModel 
  {

    private IBulkToolView _selectedView;
    public IBulkToolView SelectedView
    {
      get
      {
        return _selectedView;
      }
      set
      {
        if (_selectedView == value) return;
        _selectedView = value;
        OnPropertyChanged("SelectedView");

      }
    }
  public ObservableCollection<IBulkToolView> Views { get; set; }

    public DelegateCommand<object> AddViewCommand { get; private set; }
    public DelegateCommand<object> OpenPortfolioCommand { get; private set; }
    public DelegateCommand<object> SavePortfolioCommand { get; private set; }
    public DelegateCommand<object> GetHelpCommand { get; private set; }


    public BulkToolShellViewModel(ObservableCollection<IBulkToolView> views_)
      : this()
    {
      Views = views_;
    }

    public BulkToolShellViewModel()
    {
      Views = new ObservableCollection<IBulkToolView>();
      AddViewCommand = new DelegateCommand<object>(o_ => Views.Add(new TestView
                                                                     {
                                                                       ViewModel = new TestViewModel()
                                                                     }));
      OpenPortfolioCommand = new DelegateCommand<object>(OpenPortfolio);
      SavePortfolioCommand = new DelegateCommand<object>(SavePortfolio);
      GetHelpCommand = new DelegateCommand<object>(GetHelp);
    }

    private void GetHelp(object obj_)
    {

    }

    private void SavePortfolio(object obj_)
    {

    }

    private void OpenPortfolio(object obj_)
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged()
    {
      throw new NotImplementedException();
    }

    public void RaisePropertyChanged(string propertyName)
    {
      throw new NotImplementedException();
    }

    public string this[string columnName]
    {
      get { throw new NotImplementedException(); }
    }

    public string Error { get; private set; }
    public AsyncContext Async { get; private set; }
    public XmlLanguage Language { get; private set; }
    public string Key { get; set; }


  }

谢谢!

1 个答案:

答案 0 :(得分:2)

为什么BulkToolShellViewModel有自己的PropertyChanged事件以及什么都不做的RaisePropertyChanged方法?它不应该从BaseViewModel继承此功能吗?也许UI附加到BulkToolShellViewModel.PropertyChanged而不是BaseViewModel.PropertyChanged,并且永远不会收到更改通知。