0

我无法将 my 绑定View Model到我的View. 我是初学者MVVM,但我相信我(几乎)正确地实现了我的系统。我有一个包含数据的数据,Model我将其放入我的.View ModelView ModelView

我的问题是我的ListBox每个View项目有 3 个对象,我似乎无法为列表中的每个项目正确绑定它。

主页.xaml

<ListBox x:Name="FavoritesListBox" ItemsSource="{Binding FavoriteItems}" 
         SelectionChanged="FavoritesListBox_SelectionChanged">

    <StackPanel Orientation="Horizontal" Margin="12,0,12,0">
        <Image x:Name="favicon" Source="{Binding Favicon}" 
               Width="50" Height="50"/>
        <StackPanel>
            <TextBlock x:Name="favoritesName" Text="{Binding Name}" 
                       FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
            <TextBlock x:Name="favoritesAddress" 
                       Text="{Binding Address}" Margin="12,0,0,0"/>
        </StackPanel>
    </StackPanel>                
</ListBox>

MainPage.xaml.cs

public FavoritesPage()
    {
        InitializeComponent();

        // Set the data context of the listbox control to the sample data
        FavoritesListBox.DataContext = App.ViewModel;
    }

应用程序.xaml.cs

private static MainViewModel viewModel = null;        

    public static MainViewModel ViewModel
    {
        get
        {
            // Delay creation of the view model until necessary
            if (viewModel == null)
                viewModel = new MainViewModel();

            return viewModel;
        }
    }

主视图模型.cs

public ObservableCollection<ItemViewModel> FavoriteItems { get; private set; }

    public MainViewModel()
    {
        //FavoriteItems = new ObservableCollection<ItemViewModel>();
        FavoriteItems = Settings.FavoritesList.Value;
    }

Settings.cs(模型)

public static Setting<ObservableCollection<ItemViewModel>> FavoritesList = 
    new Setting<ObservableCollection<ItemViewModel>>(
        "Favorites", 
        new ObservableCollection<ItemViewModel>());

项目视图模型.cs

private string _favicon;
    public string Favicon
    {
        get
        {
            return _favicon;
        }
        set
        {
            if (value != _favicon)
            {
                _favicon = value;
                NotifyPropertyChanged("Favicon");
            }
        }
    }

    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (value != _name)
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    private string _address;
    public string Address
    {
        get
        {
            return _address;
        }
        set
        {
            if (value != _address)
            {
                _address = value;
                NotifyPropertyChanged("Address");
            }
        }
    }

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

..这就是我保存每个项目的位置和方式(应该在ItemViewModel

void addToFavorites_Click(object sender, EventArgs e)
{
    var favoriteItem = 
        new ItemViewModel{
            Favicon = "", 
            Name = "", 
            Address = TheBrowser.currentUrl() };
        Settings.FavoritesList.Value.Add(favoriteItem);            
}

FavoritesList使用ItemViewModel包含 3 个对象的位置填充。该列表已正确填充,因为在调试期间我可以看到 中的实体,但是我在调​​用这些实体以显示在我的?FavoritesList中时遇到问题view modelListBoxview

我相信我的绑定不正确,但我不确定如何解决这个问题?

4

2 回答 2

0

除了将您的 DataContext 设置为您的视图模型(如链接到创建 ContextBinding XAML的评论中所述)之外,您还需要让您的视图模型实现INotifyPropertyChanged(包括ItemViewModel您未在问题中显示的)

于 2012-09-18T02:15:07.987 回答
0

在您的 XAML 中,您绑定到路径Name,并且Address您的ItemViewModel?

正确阅读代码后更新:

您没有更新列表框项目的数据模板。这是你需要做的:

<ListBox x:Name="FavoritesListBox" ItemsSource="{Binding FavoriteItems}" SelectionChanged="FavoritesListBox_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="12,0,12,0">
                <Image x:Name="favicon" Source="{Binding Favicon}" Width="50" Height="50"/>
                <StackPanel>
                    <TextBlock x:Name="favoritesName" Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
                    <TextBlock x:Name="favoritesAddress" Text="{Binding Address}" Margin="12,0,0,0"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
于 2012-09-18T03:20:53.840 回答