【问题标题】:XMAL Binding and CollectionViewSource issue in WinRTWinRT 中的 XMAL 绑定和 CollectionViewSource 问题
【发布时间】:2015-12-19 10:05:27
【问题描述】:

在我的情况下,一切正常接受 IsBusy 属性,同时从 VM(视图模型)中的 Web 服务获取数据我明确更新 IsBusy = true 以在 UI 上显示进度条但它不工作。并且 propertychanged 始终为空。所以进度条可见性总是可见的,它与 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 { 私有 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

}

【问题讨论】:

    标签: c# wpf xaml windows-runtime


    【解决方案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}, ...}"
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2021-12-30
    • 2013-01-02
    相关资源
    最近更新 更多