WPF vb.Net 数据绑定 INotifyPropertyChanged
WPF vb.Net DataBinding INotifyPropertyChanged
我制作了一个小应用程序,用于练习 WPF vb.NET 框架中的数据绑定。它包含 3 个 Windows,一个带有滑块,一个带有文本框,一个带有 3 个按钮,用于打开另外两个 Windows。
MainWindow.vb:
Imports System.ComponentModel
Imports WPF_Viewmodel_Test.ViewModel_Namespace
Imports GalaSoft.MvvmLight.Command
Public Class MainWindow
Private viewModel As New ViewModel()
Public Sub New()
InitializeComponent()
End Sub
Private Sub SettingsWindowButton_OnClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim settingsWindow = New SettingsWindow()
settingsWindow.DataContext = viewModel
settingsWindow.Show()
End Sub
Private Sub BoundWindowButton_OnClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim boundWindow = New BoundWindow()
boundWindow.DataContext = viewModel
boundWindow.Show()
End Sub
End Class
Namespace ViewModel_Namespace
Public Class ViewModel
Implements INotifyPropertyChanged
#Region "Property"
Public Sub New()
End Sub
Private _fontSizeSetting As Integer = 10
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Public Property FontSizeSetting As Integer
Get
Return _fontSizeSetting
End Get
Set(value As Integer)
_fontSizeSetting = value
OnPropertyChanged("FontSizeSetting")
End Set
End Property
Protected Overridable Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
#End Region
#Region "Command"
Private _changeFontsize As ICommand
Public ReadOnly Property ChangeFontsizeCommand() As ICommand
Get
If _changeFontsize Is Nothing Then
_changeFontsize = New RelayCommand(Sub() ChangeFontsize(), Function() True)
End If
Return _changeFontsize
End Get
End Property
Private Sub ChangeFontsize()
FontSizeSetting = 50
End Sub
#End Region
End Class
End Namespace
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:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cmd="clr-namespace:WPF_Viewmodel_Test.ViewModel_Namespace"
mc:Ignorable="d"
Title="MainWindow" Height="172" Width="504">
<Window.Resources>
<cmd:ViewModel x:Key="mycommand"></cmd:ViewModel>
</Window.Resources>
<StackPanel>
<Button Content="Settings Window" Click="SettingsWindowButton_OnClick"/>
<Button Content="Bound Window" Click="BoundWindowButton_OnClick"/>
<Button Content="Change Fontsize" Click="ChangeButton_OnClick" Command="{Binding Source={StaticResource mycommand}, Path=WebSeiteAufrufenCommand}"/>
</StackPanel>
</Window>
SettingsWindow.xaml:
<Window x:Class="SettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="SettingsWindow" Height="450" Width="800" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid>
<Slider x:Name="slider1" Value="{Binding FontSizeSetting, Mode=TwoWay}" Minimum="10" Maximum="100" />
</Grid>
</Window>
BoundWindow.xaml:
<Window x:Class="BoundWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Boundwindow" Height="450" Width="800">
<Grid>
<TextBox FontSize="{Binding FontSizeSetting, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" Text="This is an Example Text"/>
</Grid>
</Window>
绑定工作正常,文本框的字体大小根据滑块值进行了调整。
现在我想通过主窗口中的按钮更改 属性 'FonSizeSetting'。 属性 由 ICommand 设置为 50。
FontSizeSetting = 50
我引发了 属性Changed 事件,但 Windows 仍然没有更新字体大小或滑块位置。我已经尝试通过 Window.DataContext 直接在 XAML 中设置 DataContext,但这导致了非工作绑定。
任何人都可以向我解释我在这里做错了什么吗?
您在 MainWindow 中有 2 个 ViewModel 实例:一个在资源 <cmd:ViewModel x:Key="mycommand"></cmd:ViewModel>
中,一个在私有字段中:Private viewModel As New ViewModel()
您从未绑定到其他资源实例的命令调用命令 windows。
像这样更改 MainWindow:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cmd="clr-namespace:WPF_Viewmodel_Test.ViewModel_Namespace"
mc:Ignorable="d"
Title="MainWindow" Height="172" Width="504">
<Window.DataContext>
<cmd:ViewModel />
</Window.DataContext>
<StackPanel>
<Button Content="Settings Window" Click="SettingsWindowButton_OnClick"/>
<Button Content="Bound Window" Click="BoundWindowButton_OnClick"/>
<Button Content="Change Fontsize" Click="ChangeButton_OnClick"
Command="{Binding Path=ChangeFontsizeCommand}"/>
</StackPanel>
</Window>
和
Public Class MainWindow
Public Sub New()
InitializeComponent()
End Sub
Private Sub SettingsWindowButton_OnClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim settingsWindow = New SettingsWindow()
settingsWindow.DataContext = DataContext
settingsWindow.Show()
End Sub
Private Sub BoundWindowButton_OnClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim boundWindow = New BoundWindow()
boundWindow.DataContext = DataContext
boundWindow.Show()
End Sub
End Class
我制作了一个小应用程序,用于练习 WPF vb.NET 框架中的数据绑定。它包含 3 个 Windows,一个带有滑块,一个带有文本框,一个带有 3 个按钮,用于打开另外两个 Windows。
MainWindow.vb:
Imports System.ComponentModel
Imports WPF_Viewmodel_Test.ViewModel_Namespace
Imports GalaSoft.MvvmLight.Command
Public Class MainWindow
Private viewModel As New ViewModel()
Public Sub New()
InitializeComponent()
End Sub
Private Sub SettingsWindowButton_OnClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim settingsWindow = New SettingsWindow()
settingsWindow.DataContext = viewModel
settingsWindow.Show()
End Sub
Private Sub BoundWindowButton_OnClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim boundWindow = New BoundWindow()
boundWindow.DataContext = viewModel
boundWindow.Show()
End Sub
End Class
Namespace ViewModel_Namespace
Public Class ViewModel
Implements INotifyPropertyChanged
#Region "Property"
Public Sub New()
End Sub
Private _fontSizeSetting As Integer = 10
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Public Property FontSizeSetting As Integer
Get
Return _fontSizeSetting
End Get
Set(value As Integer)
_fontSizeSetting = value
OnPropertyChanged("FontSizeSetting")
End Set
End Property
Protected Overridable Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
#End Region
#Region "Command"
Private _changeFontsize As ICommand
Public ReadOnly Property ChangeFontsizeCommand() As ICommand
Get
If _changeFontsize Is Nothing Then
_changeFontsize = New RelayCommand(Sub() ChangeFontsize(), Function() True)
End If
Return _changeFontsize
End Get
End Property
Private Sub ChangeFontsize()
FontSizeSetting = 50
End Sub
#End Region
End Class
End Namespace
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:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cmd="clr-namespace:WPF_Viewmodel_Test.ViewModel_Namespace"
mc:Ignorable="d"
Title="MainWindow" Height="172" Width="504">
<Window.Resources>
<cmd:ViewModel x:Key="mycommand"></cmd:ViewModel>
</Window.Resources>
<StackPanel>
<Button Content="Settings Window" Click="SettingsWindowButton_OnClick"/>
<Button Content="Bound Window" Click="BoundWindowButton_OnClick"/>
<Button Content="Change Fontsize" Click="ChangeButton_OnClick" Command="{Binding Source={StaticResource mycommand}, Path=WebSeiteAufrufenCommand}"/>
</StackPanel>
</Window>
SettingsWindow.xaml:
<Window x:Class="SettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="SettingsWindow" Height="450" Width="800" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid>
<Slider x:Name="slider1" Value="{Binding FontSizeSetting, Mode=TwoWay}" Minimum="10" Maximum="100" />
</Grid>
</Window>
BoundWindow.xaml:
<Window x:Class="BoundWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Boundwindow" Height="450" Width="800">
<Grid>
<TextBox FontSize="{Binding FontSizeSetting, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" Text="This is an Example Text"/>
</Grid>
</Window>
绑定工作正常,文本框的字体大小根据滑块值进行了调整。 现在我想通过主窗口中的按钮更改 属性 'FonSizeSetting'。 属性 由 ICommand 设置为 50。
FontSizeSetting = 50
我引发了 属性Changed 事件,但 Windows 仍然没有更新字体大小或滑块位置。我已经尝试通过 Window.DataContext 直接在 XAML 中设置 DataContext,但这导致了非工作绑定。
任何人都可以向我解释我在这里做错了什么吗?
您在 MainWindow 中有 2 个 ViewModel 实例:一个在资源 <cmd:ViewModel x:Key="mycommand"></cmd:ViewModel>
中,一个在私有字段中:Private viewModel As New ViewModel()
您从未绑定到其他资源实例的命令调用命令 windows。
像这样更改 MainWindow:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cmd="clr-namespace:WPF_Viewmodel_Test.ViewModel_Namespace"
mc:Ignorable="d"
Title="MainWindow" Height="172" Width="504">
<Window.DataContext>
<cmd:ViewModel />
</Window.DataContext>
<StackPanel>
<Button Content="Settings Window" Click="SettingsWindowButton_OnClick"/>
<Button Content="Bound Window" Click="BoundWindowButton_OnClick"/>
<Button Content="Change Fontsize" Click="ChangeButton_OnClick"
Command="{Binding Path=ChangeFontsizeCommand}"/>
</StackPanel>
</Window>
和
Public Class MainWindow
Public Sub New()
InitializeComponent()
End Sub
Private Sub SettingsWindowButton_OnClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim settingsWindow = New SettingsWindow()
settingsWindow.DataContext = DataContext
settingsWindow.Show()
End Sub
Private Sub BoundWindowButton_OnClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim boundWindow = New BoundWindow()
boundWindow.DataContext = DataContext
boundWindow.Show()
End Sub
End Class