2015-09-22 73 views
0

在我的情況下,一切工作正常接受IsBusy屬性,雖然從VM(視圖模型)中的Web服務獲取數據,我明確更新了IsBusy = true以顯示進度條在UI上,但它不工作。並且propertychanged始終爲空。所以進度條可見性總是可見的,它與IsBusy屬性綁定。請幫助我在這裏失蹤。WinRT中的XMAL綁定和CollectionViewSource問題

這裏是我的XAML代碼:

<local:StockVm x:Key="VM"/> 

    <CollectionViewSource x:Key="CVS" Source="{Binding RequestedItems, Source={StaticResource VM}}" 
          IsSourceGrouped="True" 
          ItemsPath="StockItems"/>  

</Page.Resources> 

頁面設計XAML代碼:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <TextBlock Grid.Row="0" Visibility="{Binding Path=IsBusy, Converter={StaticResource boolVis1}, ConverterParameter=true}">Loading...</TextBlock> 
    <ProgressBar x:Name="LoadingBar" Visibility="{Binding Path=IsBusy, Converter={StaticResource boolVis1}, ConverterParameter=true}" IsIndeterminate="true" Height="4" /> 

    <local:DebugListView x:Name="TheListView" Grid.Row="1" ItemsSource="{Binding Source={StaticResource CVS}}" ItemTemplate="{StaticResource ItemTemplate}" > 
     <ListView.GroupStyle> 
      <GroupStyle HeaderTemplate="{StaticResource StockHeaderTemplate}" HeaderContainerStyle="{StaticResource ListViewHeaderItemStyle}" /> 
     </ListView.GroupStyle> 
    </local:DebugListView> 

</Grid> 

.CS碼

public TestPageDev() 
     { 
      this.InitializeComponent(); 
      _view = this.Resources["VM"] as StockVm; 
      _view.LoadData();  
      this.DataContext = this; 
     } 

公共類StockVm:BindableObject { p內置的ObservableCollection _requestedItems;

public ObservableCollection<RequestedStock> RequestedItems 
    { 
     get { return _requestedItems; } 
     set { SetProperty(ref _requestedItems, value); } 
    } 

    public StockVm() 
    { 

    } 

    public async Task LoadData() 
    { 
     IsBusy = true; 
     await Task.Delay(TimeSpan.FromSeconds(5)); 
     IsBusy = false; 
    } 


    #region - Public Members - 
    private bool _IsBusy = false; 
    public bool IsBusy 
    { 
     get 
     { 
      return _IsBusy; 
     } 
     set 
     { 
      if (_IsBusy != value) 
      { 
       _IsBusy = value; 
       RaisePropertyEvent("IsBusy"); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 

    #region - INotifyPropertyChanged - 

    private void RaisePropertyEvent(string propName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
    } 
    #endregion 

} 

回答

1

你將不得不在DataContext設置爲視圖模型的實例,就像這樣:

public TestPageDev() 
{ 
    this.InitializeComponent(); 
    _view = this.Resources["VM"] as StockVm; 
    _view.LoadData();  
    this.DataContext = _view; // here 
} 

(其中_view是一個視圖模型對象相當奇怪的名字)。

或者你明確地這樣設置綁定源:

Visibility="{Binding Path=IsBusy, Source={StaticResource VM}, ...}" 
+0

我會放在'LoadData'在VM中的'Constructor'並將其放置在XAML像這樣:'<地方: '''''CollectionVm />'''''''''''CollectionViewSource x:Key =「CVS」Source =「{Binding RequestedItems}」/>'''''''' – XAMlMAX