3

重複の可能性:
WPFプロパティを変数にデータバインディングする

module1プロパティをWPFTextBox1にバインドするにはどうすればよいですか?

WPFコード:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="210,146,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>

VB.netコード:

Module Module1
    ReadOnly Property tbBinding As String
        Get
            Return "Success!"
        End Get
    End Property
End Module

以下は、私が得ているフィードバックと私が行っている読書に基づいて私が取り組んできたコードです。/ #######進行中の現在のコード(モジュールの代わりにクラスで試行)####### /

XAML:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid DataContext="Class1">
        <TextBox Height="23" HorizontalAlignment="Left" Margin="210,146,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path=tbBinding2}"/>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="192,74,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>

クラス1:

Imports System.ComponentModel

Public Class Class1
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Dim varField As String = String.Empty

    Public Property tbBinding2 As String
        Get
            Return varField
        End Get

        Set(value As String)
            varField = value
            NotifyPropertyChanged("tbBinding2")
        End Set
    End Property
End Class

メインウィンドウ:

Class MainWindow 

    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim myClass1 As New Class1
        myClass1.tbBinding2 = "Success!"
    End Sub
End Class
4

2 に答える 2

5

あなたはDataContextどこにも設定していません

WPFには、データレイヤーとUIレイヤーの2つのレイヤーがあります。データレイヤーはデフォルトであり、任意のUIオブジェクトnullのプロパティを設定することで設定できます。DataContextバインディングは、データレイヤーからUIレイヤーにデータをプルするために使用されます。

したがって、と言うとMainWindow.DataContext = new Class1()、データレイヤーbeindMainWindowをオブジェクトの新しいインスタンスに設定していることになりClass1ます。

XAMLでの書き込み<TextBox Text="{Binding tbProperty}" />は、WPFに、呼び出されたプロパティのデータレイヤーを検索し、それをの値にtbProperty使用するように指示しています。TextTextBox

として使用されているオブジェクトでを変更tbPropertyすると、その変更は(実装されている場合)にも反映されます。また、バインディングモードが(のデフォルト)に設定されている場合、に変更すると、データレイヤーのも更新されます。Class1DataContextTextBox.TextINotifyPropertyChangedTwoWayTextBox.TextTextBox.TexttbProperty

興味があれば、最近ブログに概要を投稿しました。DataContext

于 2012-07-26T19:35:54.760 に答える
0

インターフェイスを実装する必要がありINotifyPropertyChangedます。例については、この記事を参照してください。

編集:

Class1XAMLからクラス(別名「ビューモデル」)にバインドする例を次に示します。

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:viewModels:"clr-namespace:MyApplication.ViewModels"
    Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <viewModels:Class1 />
    </Window.DataContext>

    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="210,146,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path=tbBinding2}"/>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="192,74,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>

このXAMLは、クラスが名前空間Class1の下の同じアセンブリに含まれていることを前提としていることに注意してください。MyApplication.ViewModels

于 2012-07-26T17:58:51.810 に答える