WinRT中的XMAL Binding和CollectionViewSource问题

时间:2015-09-22 12:19:35

标签: c# wpf xaml windows-runtime

在我的情况下,一切正常,接受 IsBusy 属性,同时从VM(View Model)中的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; 
        }

public class StockVm:BindableObject     {         private 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 个答案:

答案 0 :(得分: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}, ...}"