使用INotifyPropertyChanged将gridview的itemSource绑定到ViewModel

时间:2019-05-24 19:15:03

标签: c# mvvm uwp binding

我希望ViewModel可以使用Test()函数遍历gridView的所有内部元素。由于某种原因,我的“ gridVariable”(请参见代码)返回null。在获取值并将其设置为TextBox时,我没有遇到任何问题。为什么GridView返回null,我该如何解决?

MainViewModel.cs

    public class MainViewModel : Observable, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public MainViewModel(){}

        private GetGridView _myGridView = new GetGridView();
        public GetGridView MyGridView
        {
            get { return _myGridView; }
        }

        public void Test()
        {
            var gridVariable = MyGridView.AnswersGrid;
            foreach (var i in gridVariable.Items) MyText.Text += "x";
        }

        private GetText _myText = new GetText();
        public GetText MyText
        {
            get { return _myText; }
            set
            {
                _myText = value;
            }
        }
    }

GetGridView.cs

    public class GetGridView : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private GridView _grid;
        public GridView AnswersGrid
        {
            get { return _grid; }
        }
    }

MainPage.xaml.cs

    public MainViewModel ViewModel { get; } = new MainViewModel();
    public MainPage()
    {
        InitializeComponent();
        this.DataContext = ViewModel;
    }

MainPage.xaml

    <RelativePanel>
        <GridView
            x:Name="GridView"
            ItemsSource="{Binding MyGridView.AnswersGrid, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
            <TextBox Text="{Binding MyText.Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  />
            <TextBox/>
        </GridView>
        <Button RelativePanel.Below="GridView" Content="butn" Click="{x:Bind ViewModel.Test}"></Button>
    </RelativePanel>

1 个答案:

答案 0 :(得分:0)

  

使用INotifyPropertyChanged将gridview的itemSource绑定到ViewModel

我检查了您的要求,如果要使用MVVM模式,则需要拆分ViewModel。我已经重写了您的代码。请检查以下内容。

ViewModel

public class MainPageViewModel : INotifyPropertyChanged
{
    public MainPageViewModel()
    {
        items = new ObservableCollection<Item>();
        items.Add(new Item { Info = "01" });
        items.Add(new Item { Info = "02" });
        items.Add(new Item { Info = "03" });
        items.Add(new Item { Info = "04" });
        items.Add(new Item { Info = "05" });
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public ObservableCollection<Item> items;
    public void Test()
    {
        foreach (var item in items)
        {
            item.Info += "X";
        }
    }

}

public class Item : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    private string _info;
    public string Info
    {
        get
        {
            return _info;
        }
        set
        {
            _info = value;
            OnPropertyChanged();
        }
    }
}

用法

<GridView
    x:Name="GridView"
    ItemsSource="{x:Bind ViewModel.items}">
    <GridView.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Info}"/>
        </DataTemplate>
    </GridView.ItemTemplate>

</GridView>
<Button RelativePanel.Below="GridView" Content="butn" Click="{x:Bind ViewModel.Test}"></Button>

有关更多详细信息,请参阅Data binding and MVVM