链接视图模型到视图

本文关键字:视图 模型 链接 | 更新日期: 2023-09-27 18:12:35

我不能得到我的xaml绑定我的视图模型。我在我的viewModel中有一个INotifyPropertyChanged类的ObservableCollection,它持有关于接收的数据。这是我的接收类:

namespace WP7SQLiteClient.Model
{
    public class MainViewModelItem : INotifyPropertyChanged
    {
        string _title, _subTitle, _imageUriPath;
        string title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                NotifyPropertyChanged("title");
            }
        }
        string subTitle
        {
            get
            {
                return _subTitle;
            }
            set
            {
                _subTitle = value;
                NotifyPropertyChanged("subTitle");
            }
        }
        string imageUriPath
        {
            get
            {
                return _imageUriPath;
            }
            set
            {
                _imageUriPath = value;
                NotifyPropertyChanged("imageUriPath");
            }
        }
        public MainViewModelItem(string title, string subtitle, string imageuripath)
        {
            this.title = title;
            this.subTitle = subtitle;
            this.imageUriPath = imageuripath;
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

和我的ViewModel,其中包含接收列表:

namespace WP7SQLiteClient.ViewModel
{
        public class PanoramaViewModel : INotifyPropertyChanged
        {
            public ObservableCollection<MainViewModelItem> _recepiesList;

            public ObservableCollection<MainViewModelItem> recepiesList
            {
                get
                {
                    return _recepiesList;
                }
                set
                {
                    _recepiesList = value;
                    NotifyPropertyChanged("recepiesList");
                }
            }
            public PanoramaViewModel()
            {
                this.recepiesList = new ObservableCollection<MainViewModelItem>();
            }
            public bool IsDataLoaded
            {
                get;
                private set;
            }
            public void LoadData()
            {
                this.recepiesList.Add(new MainViewModelItem("Classics", "", ""));
                this.recepiesList.Add(new MainViewModelItem("Perfect Pasta", "", ""));
                this.recepiesList.Add(new MainViewModelItem("Favorites", "", ""));
                this.recepiesList.Add(new MainViewModelItem("Snacks & Antipasti", "", ""));
                this.recepiesList.Add(new MainViewModelItem("Desserts", "", ""));
                this.recepiesList.Add(new MainViewModelItem("3 minutes recipes", "", ""));
                this.IsDataLoaded = true;
            }

            private string _sampleProperty = "Sample Runtime Property Value";
            /// <summary>
            /// Sample ViewModel property; this property is used in the view to display its value using a Binding
            /// </summary>
            /// <returns></returns>
            public string SampleProperty
            {
                get
                {
                    return _sampleProperty;
                }
                set
                {
                    if (value != _sampleProperty)
                    {
                        _sampleProperty = value;
                        NotifyPropertyChanged("SampleProperty");
                    }
                }
            }


            public event PropertyChangedEventHandler PropertyChanged;
            protected void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
        }
}

在我的主页。xaml(它在项目的根,viewModel是在viewModel文件夹和模型是在模型文件夹)我有我的列表框声明如下:

<ListBox x:Name="recepiesList"  ItemTemplate="{StaticResource ListViewModelTemplate}" > 
                </ListBox>

模板位于App.xaml中,并且肯定是正确的。它使用{Binding title}

在MainPage.cs我尝试链接视图模型与页面使用:

public static PanoramaViewModel viewModel = null;
        public static PanoramaViewModel ViewModel
        {
            get
            {
                // Delay creation of the view model until necessary
                if (viewModel == null)
                    viewModel = new PanoramaViewModel();
                return viewModel;
            }
        } 
public MainPage()
        {
            InitializeComponent();
            ViewModel.LoadData();
            DataContext = ViewModel;
        }

但是它不起作用,调试器也不会抛出错误。我怎样才能正确地链接视图模型与xaml ?

UPDATE我的模板是这样的:

<DataTemplate x:Key="ListViewModelTemplate"> <!-- for recent recepies-->
            <Grid Width="400" Height="80" VerticalAlignment="Center">
                <StackPanel Orientation="Vertical">
                    <Border CornerRadius="0" x:Name="brdTesat" BorderBrush="Black" BorderThickness="1" Width="80" Height="80">
                    <Border.Background>
                        <ImageBrush x:Name="backgroundImaageBrush" Stretch="Fill">
                            <ImageBrush.ImageSource>
                                    <BitmapImage x:Name="bmapBackground" UriSource="{Binding imageUriPath}" >
                                </BitmapImage>
                            </ImageBrush.ImageSource>
                        </ImageBrush>
                    </Border.Background>
                </Border>
                    <StackPanel>
                    <TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="{Binding title}" TextWrapping="Wrap"></TextBlock>
                        <TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="DA" TextWrapping="Wrap"></TextBlock>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </DataTemplate>

更新2

我把列表框代码改成:

<ListBox x:Name="recepiesList"  ItemsSource="{Binding recepiesList}" >             </ListBox>

没有模板,我得到了[project_name].Model.MainViewModelItem的列表,所以我认为模板有问题。我做错了什么?

链接视图模型到视图

您需要将ListBox绑定到数据。所以,这应该对你有用。

<ListBox x:Name="recepiesList" ItemsSource="{Binding recepiesList}" ItemTemplate="{StaticResource ListViewModelTemplate}"  />

我们在项目中使用MEF,并将视图模型与view . example .cs中的以下代码链接起来:

[Import]
public ConnectionStringSetupViewModel ViewModel
{
    get { return DataContext as ConnectionStringSetupViewModel; }
    set { DataContext = value; }
}

这允许在创建目录时满足导入。如果您不使用MEF,您可以使用上面相同的代码而不需要导入,但是当您创建视图时,您必须为它分配一个视图模型类的新实例。