INotifyPropertyChanged
인터페이스를 구현하는 기본 클래스를 만들었습니다. 이 클래스는 또한 모든 속성의 값을 설정하고 필요한 경우 PropertyChanged
이벤트를 발생시키는 일반 함수 SetProperty
을 포함합니다.ByRef 매개 변수로 속성 대 변수
Public Class BaseClass
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Function SetProperty(Of T)(ByRef storage As T, value As T, <CallerMemberName> Optional ByVal propertyName As String = Nothing) As Boolean
If Object.Equals(storage, value) Then
Return False
End If
storage = value
Me.OnPropertyChanged(propertyName)
Return True
End Function
Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName As String = Nothing)
If String.IsNullOrEmpty(propertyName) Then
Throw New ArgumentNullException(NameOf(propertyName))
End If
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
그렇다면 일부 데이터를 보유하고있는 클래스가 있습니다. 단순화를 위해 여기에는 하나의 특성 만 포함됩니다 (이 예제에서).
Public Class Item
Public Property Text As String
End Class
그렇다면 기본 클래스에서 상속 받고 데이터 보관 클래스를 사용하는 세 번째 클래스가 있습니다. 이 세 번째 클래스는 WPF 윈도우의 ViewModel입니다.
RelayCommand
클래스의 코드는 나와 있지 않습니다. 아마도 모든 구현은 직접 구현되어 있기 때문입니다. 이 클래스는 명령이 실행될 때 주어진 함수를 실행한다는 것을 명심하십시오.
Public Class ViewModel
Inherits BaseClass
Private _text1 As Item 'data holding class
Private _text2 As String 'simple variable
Private _testCommand As ICommand = New RelayCommand(AddressOf Me.Test)
Public Sub New()
_text1 = New Item
End Sub
Public Property Text1 As String
Get
Return _text1.Text
End Get
Set(ByVal value As String)
Me.SetProperty(Of String)(_text1.Text, value)
End Set
End Property
Public Property Text2 As String
Get
Return _text2
End Get
Set(ByVal value As String)
Me.SetProperty(Of String)(_text2, value)
End Set
End Property
Public ReadOnly Property TestCommand As ICommand
Get
Return _testCommand
End Get
End Property
Private Sub Test()
Me.Text1 = "Text1"
Me.Text2 = "Text2"
End Sub
End Class
그리고 나는 그것의 DataContext
과 뷰 모델 클래스를 사용하여 내 WPF 창을 가지고있다.
<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:local="clr-namespace:WpfTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Text1}" Height="24" Width="100" />
<TextBox Text="{Binding Text2}" Height="24" Width="100" />
<Button Height="24" Content="Fill" Command="{Binding TestCommand}" />
</StackPanel>
</Window>
이 창에는 두 개의 텍스트 상자와 버튼 만 있습니다. TextBoxes는 Text1
및 Text2
속성에 바인딩되며 단추는 TestCommand
명령을 실행하기로되어 있습니다.
명령을 실행하면 Text1
및 Text2
속성 모두 값이 제공됩니다. 두 속성 모두 PropertyChanged
이벤트를 발생시키기 때문에 이러한 값은 내 창에 표시되어야합니다.
그러나 "Text2"값만 내 창에 표시됩니다.
속성의 값이 Text1
인데 "Text1"이지만이 속성에 대한 PropertyChanged
이벤트가 속성 값보다 높아진 것 같습니다.
내 기본 클래스에서 SetProperty
함수를 변경하여 속성 값을 얻은 후 PropertyChanged
을 발생시킬 수 있습니까?
도움 주셔서 감사합니다.
http://stackoverflow.com/a/4520101/17034 –