gpt4 book ai didi

c# - 使用 WPF 和 MVVM 模式将值从子窗口传递到父窗口

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:47 35 4
gpt4 key购买 nike

我有一个父窗口,其中包含名为“SchoolName”的文本框和一个名为“Lookup school Name”的按钮。

该按钮打开一个包含学校名称列表的子窗口。现在,当用户从子窗口中选择学校名称,并单击“使用所选学校”按钮时。我需要在父 View 的文本框中填充所选学校。

注意:我采纳了 Sam 和其他人的建议来使这段代码工作。我已经更新了我的代码,以便其他人可以简单地使用它。

SelectSchoolView.xaml(父窗口)

<Window x:Class="MyProject.UI.SelectSchoolView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Parent" Height="202" Width="547">
<Grid>
<TextBox Height="23" Width="192"
Name="txtSchoolNames"
Text="{Binding Path=SchoolNames, UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}"
/>

<Label Content="School Codes" Height="28" HorizontalAlignment="Left"
Margin="30,38,0,0" Name="label1" VerticalAlignment="Top" />
<Button Content="Lookup School Code" Height="30" HorizontalAlignment="Left"
Margin="321,36,0,0" Name="button1" VerticalAlignment="Top" Width="163"
Command="{Binding Path=DisplayLookupDialogCommand}"/>
</Grid>
</Window>

SchoolNameLookup.xaml(查找学校名称的子窗口)

<Window x:Class="MyProject.UI.SchoolNameLookup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="SchoolCodeLookup" Height="335" Width="426">

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="226*" />
<RowDefinition Height="70*" />
</Grid.RowDefinitions>

<toolkit:DataGrid Grid.Row="0" Grid.Column="1" x:Name="dgSchoolList"
ItemsSource="{Binding Path=SchoolList}"
SelectedItem="{Binding Path=SelectedSchoolItem, Mode=TwoWay}"
Width="294"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserResizeRows="False"
CanUserSortColumns="True"
SelectionMode="Single">

<Button Grid.Row="1" Grid.Column="1" Content="Use Selected School Name"
Height="23" Name="btnSelect" Width="131" Command="{Binding
Path=UseSelectedSchoolNameCommand}" />
</Grid>
</Window>

SchoolNameLookupViewModel

   private string _schoolNames;
public string SchoolNames
{
get { return _schoolNames; }
set
{
_schoolNames= value;
OnPropertyChanged(SchoolNames);
}
}

private ICommand _useSelectedSchoolNameCommand;
public ICommand UseSelectedSchoolNameCommand{
get
{
if (_useSelectedSchoolNameCommand== null)
_useSelectedSchoolNameCommand= new RelayCommand(a =>
DoUseSelectedSchollNameItem(), p => true);
return _useSelectedSchoolNameCommand;
}
set
{
_useSelectedSchoolNameCommand= value;
}

}

private void DoUseSelectedSchoolNameItem() {
StringBuilder sfiString = new StringBuilder();
ObservableCollection<SchoolModel> oCol =
new ObservableCollection<SchoolModel>();
foreach (SchoolModel itm in SchollNameList)
{
if (itm.isSelected) {
sfiString.Append(itm.SchoolName + "; ");
_schoolNames = sfiString.ToString();
}
}
OnPropertyChanged(SchoolNames);
}

private ICommand _displayLookupDialogCommand;
public ICommand DisplayLookupDialogCommand
{
get
{
if (_displayLookupDialogCommand== null)
_displayLookupDialogCommand= new
RelayCommand(a => DoDisplayLookupDialog(), p => true);
return _displayLookupDialogCommand;
}
set
{
_displayLookupDialogCommand= value;
}
}

private void DoDisplayLookupDialog()
{
SchoolNameLookup snl = new SchoolNameLookup();
snl.DataContext = this; //==> This what I was missing. Now my code works as I was expecting
snl.Show();
}

最佳答案

我的解决方案是将两个窗口绑定(bind)到同一个 ViewModel,然后定义一个属性来保存代码的结果值,我们称之为 CurrentSchoolCodes,将标签绑定(bind)到该属性。确保 CurrentSchoolCodes 引发 INotifyPropertyChanged 事件。 然后在 DoUseSelectedSchoolNameItem 中设置 CurrentSchoolCodes 的值。

对于模型中的属性,我建议您在需要时加载它们(延迟加载模式)。我用这个方法检查你的属性的 get 访问器是否仍然为 null,加载并为其分配值。代码就像这个代码片段:

private ObservableCollection<SchoolModel> _schoolList;
public ObservableCollection<SchoolModel> SchoolList{
get {
if ( _schoolList == null )
_schoolList = LoadSchoolList();
return _schoolList;
}
}

通过这种方式,绑定(bind)到此 SchoolList 属性的 WPF 控件第一次尝试获取此属性的值时,该值将被加载和缓存,然后返回。

注意:我不得不说这种属性应该谨慎使用,因为加载数据可能是一个耗时的过程。最好在后台线程中加载数据以保持 UI 响应。

关于c# - 使用 WPF 和 MVVM 模式将值从子窗口传递到父窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7507527/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com