在WPF中绑定多个类

huangapple go评论77阅读模式
英文:

Binding multiples Class in Wpf

问题

我无法执行代码部分的翻译,请参考以下已经翻译好的内容:

  1. "I can't link 2 TextBoxes to properties defined in 2 classes."

    • "我无法将两个文本框链接到两个类中定义的属性。"
  2. "I tested this simple example Where is the error, please?"

    • "我测试了这个简单的示例,请问错误在哪里?"
  3. "My XAML:"

    • "我的XAML:"
  4. "The MainWindow:"

    • "主窗口:"
  5. "My classes"

    • "我的类"

请注意,这只是已经翻译的部分,不包括代码部分。如果您需要对代码部分进行翻译,请提供具体的代码段。

英文:

I can't link 2 TextBoxes to properties defined in 2 classes.
I tested this simple example
Where is the error, please?

My 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>

The MainWindow:

    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

My classes

    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

答案1

得分: 0

在你的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

英文:

In your MainWindow's constructor, you are providing a new DataContext to your StackPanels (SP1 and SP2):

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

Every control in each StackPanel inherits the DataContext of the StackPanel, not the MainWindow's DataContext. Your TextBlock bindings should therefore be adjusted accordingly:

&lt;StackPanel Grid.Row=&quot;0&quot; x:Name=&quot;SP1&quot; Orientation=&quot;Horizontal&quot; &gt;
	&lt;TextBlock Text=&quot;{Binding Text1}&quot;/&gt;
&lt;/StackPanel&gt;
&lt;StackPanel Grid.Row=&quot;1&quot; x:Name=&quot;SP2&quot; Orientation=&quot;Horizontal&quot;&gt;
	&lt;TextBlock Text=&quot;{Binding Text2}&quot;/&gt;
&lt;/StackPanel&gt;

The bindings you currently have are the equivalent to ViewModels.ViewMod1.ViewModels.ViewMod1.Text1 and ViewModels.ViewMod2.ViewModels.ViewMod2.Text2.

huangapple
  • 本文由 发表于 2023年7月11日 06:21:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定