将数据绑定到WPF表单,MVVM

时间:2014-10-13 08:15:29

标签: wpf vb.net xaml mvvm

我正在尝试使用MVVM patern将数据绑定到WPF表单。但我尝试了不同的方法,但我没有让任何人正常工作。那么有人可以为我提供解决方案吗?

MainWindow.xaml

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MoneyManager.WPF.ViewModels"
WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="500" Width="300">
<Window.Resources>
    <DataTemplate x:Key="CategoryModelTemplate">
        <StackPanel>
            <TextBlock Text="{Binding CategoryName}" />
            <TextBlock Text="{Binding CategoryType}" FontSize="7"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<Grid Margin="0,60">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Column="0" Grid.Row="0" Text="Categories" FontWeight="Bold" VerticalAlignment="Center" Margin="20,0"/>
    <ListBox Name="CategoriesListBox" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding Categories}" ItemTemplate="{DynamicResource CategoryModelTemplate}" HorizontalAlignment="left" Margin="20,0,0,0" Grid.RowSpan="4" Width="150" />
    <Button Content="Add " HorizontalAlignment="Left" Height="22" Grid.Row="1" VerticalAlignment="Top" Width="129.333" FontSize="9.333" Grid.Column="1" Margin="0,18,0,0"/>
    <Button Content="Delete selected" HorizontalAlignment="Left" Height="22" Grid.Row="4" VerticalAlignment="Top" Width="129.333" FontSize="9.333" Grid.Column="1" Margin="0,18,0,0"/>
    <TextBox Name="CategoryField" HorizontalAlignment="Left" Height="22" Grid.Row="2" TextWrapping="Wrap" Text="Category" VerticalAlignment="Top" Width="129.333" FontWeight="ExtraLight" Grid.Column="1" FontSize="9.333"/>
    <RadioButton Name="CategoryType" Content="Income" HorizontalAlignment="Left" Height="16" Margin="0,24,0,0" Grid.Row="2" VerticalAlignment="Top" Width="71.322" FontSize="9.333" Grid.Column="1" GroupName="CategoryType" IsChecked="True"/>
    <RadioButton Content="Expense" HorizontalAlignment="Left" Height="16" Margin="76.322,24,0,0" Grid.Row="2" VerticalAlignment="Top" Width="71.322" Grid.Column="1" FontSize="9.333" GroupName="CategoryType"/>
</Grid>
</Window>

ViewModel:CategoryViewModel

Public Class CategoryViewModel
    Inherits BaseViewModel

    Private _categories As ObservableCollection(Of CategoryModel)
    Private ReadOnly _unitOfWork As UnitOfWork

    Public Sub New()
        _unitOfWork = New UnitOfWork()
    End Sub

    Public Sub Init()
        Dim obscategories = _unitOfWork.GetCategoryRepository().GetAll()
        Dim observable = New ObservableCollection(Of CategoryModel)()
        For Each c As Category In obscategories
            observable.Add(New CategoryModel(c.CategoryName, c.CategoryType))
        Next

        Categories = observable
    End Sub

    Public Property Categories As ObservableCollection(Of CategoryModel)
        Get
            Return _categories
        End Get
        Set(value As ObservableCollection(Of CategoryModel))
            _categories = value
            RaisePropertyChanged("Category")
        End Set
    End Property

    Public Sub AddCategory(categoryName As String, categoryType As Boolean)
        Dim cat = New CategoryModel(categoryName:=categoryName, categoryType:=categoryType)
        _categories.Add(cat)
        _unitOfWork.CategoryRepository.Add(cat.ToCategory())
        _unitOfWork.Commit()
    End Sub

    Public Sub RemoveCategory(removedIndex As Integer)
        Dim cat = _categories.ElementAt(removedIndex)
        Dim entity = _unitOfWork.CategoryRepository.GetById(cat.Id)
        _unitOfWork.CategoryRepository.Delete(entity)
        _categories.RemoveAt(removedIndex)
        _unitOfWork.Commit()
    End Sub
End Class

我将其余代码添加到以下要点: Gist code

1 个答案:

答案 0 :(得分:1)

假设DataContext相应地设置为CategoryViewModel属性中的Categories

RaisePropertyChanged("Category")

虽然应该提出事件的确切名称

RaisePropertyChanged("Categories")

因此ListBox永远不会通知您创建新列表