将数据绑定到WPF表单,MVVM

将数据绑定到WPF表单,MVVM,wpf,vb.net,xaml,mvvm,Wpf,Vb.net,Xaml,Mvvm,我正在尝试使用MVVM模式将数据绑定到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-name

我正在尝试使用MVVM模式将数据绑定到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>
我将代码的其余部分添加到以下要点中:

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

RaisePropertyChanged("Category")
同时,应为财产的确切名称提出事件

RaisePropertyChanged("Categories")

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

请尝试调试您的代码,并确保视图的DataContext设置正确。如果可以,请从IDE中查看输出窗口,确保没有绑定错误。
RaisePropertyChanged("Categories")