【问题标题】:Binding data to WPF form, MVVM将数据绑定到 WPF 表单、MVVM
【发布时间】:2014-10-13 08:15:29
【问题描述】:

我正在尝试使用 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>

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

【问题讨论】:

  • 尝试调试您的代码并确保您的视图的 DataContext 设置正确。如果可行,请查看 IDE 的输出窗口并确保没有绑定错误。

标签: wpf vb.net xaml mvvm


【解决方案1】:

假设DataContextCategories 属性中相应地设置为CategoryViewModel

RaisePropertyChanged("Category")

虽然应该针对属性的确切名称引发事件

RaisePropertyChanged("Categories")

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

【讨论】:

    猜你喜欢
    • 2011-09-26
    • 2015-05-24
    • 1970-01-01
    • 2019-06-23
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2013-08-05
    相关资源
    最近更新 更多