WinRT中的xml绑定和CollectionViewSource问题
本文关键字:CollectionViewSource 问题 绑定 中的 xml WinRT | 更新日期: 2023-09-27 18:14:09
在我的情况下一切都很好接受IsBusy属性,而从VM(视图模型)中的web服务获取数据时,我显式更新IsBusy = true以显示UI上的进度条,但它不工作。propertychanged总是null。进度条的可见性总是可见的它与IsBusy属性绑定。请帮助我在这里遗漏了什么。
下面是我的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{
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
}
必须将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}, ...}"