英文:
Binding multiples Class in Wpf
问题
我无法执行代码部分的翻译,请参考以下已经翻译好的内容:
-
"I can't link 2 TextBoxes to properties defined in 2 classes."
- "我无法将两个文本框链接到两个类中定义的属性。"
-
"I tested this simple example Where is the error, please?"
- "我测试了这个简单的示例,请问错误在哪里?"
-
"My XAML:"
- "我的XAML:"
-
"The MainWindow:"
- "主窗口:"
-
"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.Text1
和ViewModels.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:
<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>
The bindings you currently have are the equivalent to ViewModels.ViewMod1.ViewModels.ViewMod1.Text1
and ViewModels.ViewMod2.ViewModels.ViewMod2.Text2
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论