Binding Pivot control with Observable Collection MVVM (windows phone 8)

前端 未结 3 552
北海茫月
北海茫月 2021-01-19 17:22

I\'m new to WP8 & MVVM. I created wp8 app which requests various bits of data once a user has logged in. I just can\'t get my pivots header to get created dynamically an

相关标签:
3条回答
  • 2021-01-19 17:58

    This all looks fine to me, so I'm thinking that App.MainViewModel.Pivots is null or empty when the Constructor is called (therefore the Pivot control is empty), and that you end up creating a new instance of Pivots in your MainViewModel after the MainPivotViewModel is instantiated.

    Have you tried putting a break-point in the getter for MainPivotViewModel.Pivots to confirm that there are some items?

    0 讨论(0)
  • 2021-01-19 18:11

    Problem resolved!!!

    My code was right all along but the XAML wasn't!

    Steep and painful learning curve I guess! Anyway, I found a resolution after finding an article on stackoverflow which basically showed me that the way I wrote the xaml was just not appropriate.

    I'll be honest, I don't understand why this doesn't work the way it was defined but in short, I have to use HeaderTemplate and ItemTemplate in order to display the data correctly when binded to a ViewModel!

    Here is the post: Databound pivot is not loading the first PivotItem in Windows Phone 8

    0 讨论(0)
  • 2021-01-19 18:20

    No you answer is wrong and your code is wrong.

    Error 1:

        set
        {
            if (_pivots != value) this.SetProperty(ref this._pivots, value);
        }
    

    in here it dos not matter if you change the property or the variable the binding will be lost.

    Error 2: All UIElements derived from ItemsControl ignore the INotifyPropertyChanged because it does not update the ItemsSource just the DataContext.

    Working Example

        public ObservableCollection<string> LstLog { get; set; }
        private ObservableCollection<string> _lstContent = new ObservableCollection<string>();
        public ObservableCollection<string> LstContent
        {
            get
            {
                LstLog.Add("get");
                return _lstContent;
            }
            set
            {
                LstLog.Add("set");
                _lstContent = value;
            }
        }
        public MainWindow()
        {
            LstLog = new ObservableCollection<string>();
    
            InitializeComponent();
            this.DataContext = this;
        }
    
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            LstContent.Add("Value added");
        }
    
        private void New_Click(object sender, RoutedEventArgs e)
        {
            _lstContent = new ObservableCollection<string>();
        }
    
        private void NewBind_Click(object sender, RoutedEventArgs e)
        {
            _lstContent = new ObservableCollection<string>();
            listObj.ItemsSource = _lstContent;
        }
    
        private void NewProp_Click(object sender, RoutedEventArgs e)
        {
            LstContent = new ObservableCollection<string>();
        }
    
        private void NewPropBind_Click(object sender, RoutedEventArgs e)
        {
            LstContent = new ObservableCollection<string>();
            listObj.ItemsSource = LstContent;
        }
    

    and the ui

    <Grid DataContext="{Binding}">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition Height="25" />
        </Grid.RowDefinitions>
    
        <ItemsControl Grid.Row="0" Name="logObj" ItemsSource="{Binding Path=LstLog}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <ItemsControl Grid.Row="1" Name="listObj" ItemsSource="{Binding Path=LstContent}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <StackPanel Grid.Row="2" Orientation="Horizontal">
            <Button Name="Add" Content="Add" Click="Add_Click"/>
            <Button Name="New" Content="New" Click="New_Click"/>
            <Button Name="NewBind" Content="New Bind" Click="NewBind_Click"/>
            <Button Name="NewProp" Content="New Prop" Click="NewProp_Click"/>
            <Button Name="NewPropBind" Content="New Prop Bind" Click="NewPropBind_Click"/>
        </StackPanel>
    </Grid>
    

    the LstLog is just to see the event list, if you click add it will update the two list but if click the New or the New Prop the binding is lost until you update it like in the New Bind or New Prop Bind

    Hope this will clarify the XAML List event recurrent problem.

    PS: this is in WPF but workes the same in WP8 and windows store app.

    0 讨论(0)
提交回复
热议问题