链接viewmodel查看

时间:2011-10-28 13:32:48

标签: c# silverlight windows-phone-7

我无法让我的xaml与我的viewmodel绑定。我在我的viewModel中有一个INotifyPropertyChanged类的ObservableCollection,它保存有关recepie的数据。这是我的Recepie课程:

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));
                }
            }
        }

}

在我的MainPage.xaml中(它位于项目的根目录中,viewModel位于ViewModel文件夹中,Model位于Model文件夹中)我的listbox声明如下:

<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;

        }

但它不起作用,调试器也不会引发错误。如何正确链接viewmodel和xaml?

更新我的模板如下所示:

<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的列表,所以我认为模板有问题。我做错了什么?

2 个答案:

答案 0 :(得分:2)

您需要将ListBox绑定到数据。所以,这应该适合你。

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

答案 1 :(得分:0)

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

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

这允许在创建目录时满足导入。如果您没有使用MEF,则可以在不导入的情况下使用上面相同的代码,但是在创建视图时,您必须为其分配一个viewmodel类的新实例。