私は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
次に、いくつかのデータを保持するクラスがあります。簡単にするために、このプロパティには1つのプロパティ(この例では)のみが含まれています。
Public Class Item
Public Property Text As String
End Class
私は基本クラスから継承し、データ保持クラスを使用する3番目のクラスを持っています。この3番目のクラスは、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
としてのViewModelクラスを使用して、私の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>
ご覧のとおり、このウィンドウには2つのテキストボックスと1つのボタンしか含まれていません。 TextBoxesはプロパティText1
とText2
にバインドされており、ボタンはコマンドTestCommand
を実行することになっています。
コマンドが実行されると、プロパティーText1
とText2
の両方に値が与えられます。そして、両方のプロパティがPropertyChanged
イベントを発生させるので、これらの値は私のウィンドウに表示されるはずです。
ただし、値 "Text2"のみが私のウィンドウに表示されます。
プロパティText1
の値は "Text1"ですが、プロパティが値を取得する前にこのプロパティのPropertyChanged
イベントが発生しているようです。
プロパティが値を取得した後PropertyChanged
を呼び出すために私の基本クラスのSetProperty
関数を変更する方法はありますか?
ありがとうございました。
http://stackoverflow.com/a/4520101/17034 –