XAML 绑定Wpf中的多个类

oiopk7p5  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(125)

我无法将两个文本框链接到两个类中定义的属性。我测试了这个简单的例子,请问错误在哪里?
我的XAML:

<Window x:Class="MainWindow"

        xmlns:local="clr-namespace:WpfApp_multiBinding" xmlns:multipleviewmodel="clr-namespace:WpfApp_multiBinding.MultipleViewModel" d:DataContext="{d:DesignInstance Type=local:MainWindow}"
        mc:Ignorable="d"
        Title="MainWindow">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" x:Name="SP1" Orientation="Horizontal" >
            <TextBlock  Text="{Binding ViewModels.ViewMod1.Text1}"/>
        </StackPanel>
        <StackPanel Grid.Row="1" x:Name="SP2" Orientation="Horizontal">
            <TextBlock Text="{Binding ViewModels.ViewMod2.Text2}"/>
        </StackPanel>
    </Grid>
</Window>

字符串
主窗口:

Imports WpfApp_multiBinding.MultipleViewModel
    
    Class MainWindow
            Public ViewModels As ViewModels
    
            Sub New()
                InitializeComponent()
    
                ViewModels = New ViewModels
                DataContext = ViewModels
    
                SP1.DataContext = ViewModels.ViewMod1
                SP2.DataContext = ViewModels.ViewMod2
    
                ViewModels.ViewMod1.Text1 = "toto"
                ViewModels.ViewMod2.Text2 = "titi"
            End Sub
        End Class


我的课

Imports System.ComponentModel
    
    Namespace MultipleViewModel
    
        Public Class ViewModels
            Public ViewMod1 As New ViewModel1
            Public ViewMod2 As New ViewModel2
        End Class
    
        Public Class ViewModel1
    
            Implements INotifyPropertyChanged
            Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    
            Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
                If PropertyChangedEvent IsNot Nothing Then
                    RaiseEvent PropertyChanged(Me, e)
                End If
            End Sub
    
            Private Property _Text1 As String
    
            Public Property Text1 As String
                Get
                    Return _Text1
                End Get
                Set(value As String)
                    _Text1 = value
                    OnPropertyChanged(New PropertyChangedEventArgs(NameOf(Text1)))
                End Set
            End Property
    
        End Class
    Public Class ViewModel2
... idem Class VieModel1 ...
End Class
    End Namespace

qlfbtfca

qlfbtfca1#

在MainWindow的构造函数中,您将为StackPanels(SP1和SP2)提供一个新的DataContext:

SP1.DataContext = ViewModels.ViewMod1
SP2.DataContext = ViewModels.ViewMod2

字符串
每个StackPanel中的每个控件都继承StackPanel的DataContext,而不是MainWindow的DataContext。因此,您的TextBlock绑定应进行相应的调整:

<StackPanel Grid.Row="0" x:Name="SP1" Orientation="Horizontal" >
    <TextBlock Text="{Binding Text1}"/>
</StackPanel>
<StackPanel Grid.Row="1" x:Name="SP2" Orientation="Horizontal">
    <TextBlock Text="{Binding Text2}"/>
</StackPanel>


您当前拥有的绑定相当于ViewModels.ViewMod1.ViewModels.ViewMod1.Text1ViewModels.ViewMod2.ViewModels.ViewMod2.Text2

相关问题