簡體   English   中英

WPF vb.Net 數據綁定 INotifyPropertyChanged

[英]WPF vb.Net DataBinding INotifyPropertyChanged

我制作了一個小應用程序,用於在 WPF vb.NET Framework 中練習數據綁定。 它包含 3 個窗口,一個帶有滑塊,一個帶有文本框,一個帶有 3 個打開其他兩個窗口的按鈕。

主窗口.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>

設置窗口.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

我引發了 PropertyChanged 事件,但兩個 Windows 仍然沒有更新字體大小或滑塊位置。 我已經嘗試通過 Window.DataContext 直接在 XAML 中設置 DataContext,但這導致了非工作綁定。

誰能向我解釋我在這里做錯了什么?

您在 MainWindow 中有 2 個 ViewModel 實例:一個在 Resources <cmd:ViewModel x:Key="mycommand"></cmd:ViewModel>中,一個在私有字段中: Private viewModel As New ViewModel()

您從不綁定到其他窗口的資源實例調用命令。

像這樣更改 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM