【发布时间】:2019-08-31 02:46:43
【问题描述】:
我正在制作一个窗口来管理使用笔记本电脑的用户。我有一个名为“LaptopWindow”的窗口,其中包含一个文本框来显示使用它的用户的用户 ID。我制作了一个按钮来打开名为“FindEmployeeUC”的新用户控件,通过选择用户控件的 DataGrid 中的行并将其传递回“笔记本电脑窗口”中的文本框来查找“EmpID”。
我获得了 DataGrid 的选定行,并使用属性名称“SelectedUA”将其保存在视图模型“UserAccountViewModel”中。
当 OnPropertyChanged 事件触发时,我调用“LaptopManagementViewModel”实例(此视图模型与“LaptopWindow”绑定)并通过名为“ReceiverID”的属性将 EmpID 设置为“LaptopWindow”中的 TextBox
“ReceiverID”属性获得了值,但“LaptopWindow”的 UI 没有更新。
我尝试使用Delegate,Singleton模式,结果相同。
这里有一些代码来解释我所面临的更多问题
- “LaptopWindow”xaml:
<StackPanel Grid.Row="2" Grid.Column="1" Style="{StaticResource inputControl}">
<TextBlock Text="Người nhận"/>
<TextBox Name="txtReceiver" Text="{Binding ReceiverID,Source={StaticResource vmLaptopManagement}}" Margin="0,0,30,0"/>
</StackPanel>
<!--Button open FindEmpUC -->
<Button Grid.Row="2" Grid.Column="1" Width="30" Height="29" HorizontalAlignment="Right" VerticalAlignment="Bottom" Background="Transparent" Margin="0,4,4,4" Command="{Binding CmdFindEmp}">
<Image Source="/imgs/find-48.png" Stretch="Uniform" />
</Button>
- “LaptopManagementViewModel”:
//the userAccountVM
UserAccountViewModel userAccountVM;
//the constructor
public LaptopManagementViewModel(UserAccountViewModel userAccountVM)
{
LstDVUS = LaptopManagementBLL.Instance.GetDVUsageStatuses();
LstLaptop = LaptopManagementBLL.Instance.GetLaptopsInfo();
this.userAccountVM = userAccountVM;
ReceiverID = this.userAccountVM.SelectedUA.EmpID;
}
//the ReceiverID property
string receiverID;
public string ReceiverID
{
get { return receiverID; }
set
{
receiverID = value;
OnPropertyChanged("ReceiverID");
}
}
//function open FindEmployeeUC
private void FindEmployee(object obj)
{
//show findEmployee UC
Window wd = new Window()
{
Content = new FindEmployeeUC(),
};
wd.ShowDialog();
}
- “FindEmployeeUC”xaml:
<DataGrid Grid.Row="1" ItemsSource="{Binding LstUA}" CanUserAddRows="False" SelectedItem="{Binding SelectedUA,Mode=TwoWay}" AutoGenerateColumns="False" ColumnWidth="*" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}"></DataGridTextColumn>
<DataGridTextColumn Header="EmpID" Binding="{Binding EmpID}"></DataGridTextColumn>
<DataGridTextColumn Header="EmpName" Binding="{Binding EmpName}"></DataGridTextColumn>
<DataGridTextColumn Header="Position" Binding="{Binding Position}"></DataGridTextColumn>
<DataGridTextColumn Header="LineGroup" Binding="{Binding LineGroup}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
- “UserAccountViewModel”:
//The property "SelectedUA"
UserAccountModel selectedUA;
public UserAccountModel SelectedUA
{
get { return selectedUA; }
set
{
if(selectedUA!=value)
{
selectedUA = value;
LaptopManagementViewModel laptopVM = new LaptopManagementViewModel(this);
OnPropertyChanged("SelectedUA");
}
}
}
我希望在“LaptopWindow”中获得 TextBox 的 EmpID。我附上一张图片以获取更多详细信息: 提前致谢!
【问题讨论】: