Wpf 是否在父窗口列表框中添加项?
几乎所有的话都是在问题标题中说的。我有一个窗口中的动物列表框和“添加新动物”按钮。当我点击该按钮时,会出现一个新窗口,我将在其中输入动物的必要日期,在“保存动物”按钮上,我想将该动物添加到第一个窗口的列表框中Wpf 是否在父窗口列表框中添加项?,wpf,xaml,listbox,window,Wpf,Xaml,Listbox,Window,几乎所有的话都是在问题标题中说的。我有一个窗口中的动物列表框和“添加新动物”按钮。当我点击该按钮时,会出现一个新窗口,我将在其中输入动物的必要日期,在“保存动物”按钮上,我想将该动物添加到第一个窗口的列表框中 <Window x:Class="HelloZooWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.mic
<Window x:Class="HelloZooWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:model="clr-namespace:HelloZoo.Model;assembly=HelloZoo.Model"
xmlns:helloZooWpf="clr-namespace:HelloZooWPF"
Title="Hello Zoo Wpf"
Height="500"
Width="525">
<Window.DataContext>
<model:Zoo Name="Belgrade Zoo">
<model:Zoo.Animals>
<model:Lion Name="Zeus" Weight="900" ImagePath="pack://application:,,,/Images/Lion.jpg"/>
<model:Lion Name="Simba" Weight="200" ImagePath="pack://application:,,,/Images/Lion.jpg"/>
<model:Antelope Name="Daisy" Weight="200" ImagePath="pack://application:,,,/Images/antelope.jpg"/>
<model:Antelope Name="Rosie" Weight="200" ImagePath="pack://application:,,,/Images/antelope.jpg"/>
<model:Antelope Name="Goldie" Weight="200" ImagePath="pack://application:,,,/Images/antelope.jpg"/>
</model:Zoo.Animals>
</model:Zoo>
</Window.DataContext>
<Grid>
<ListBox ItemsSource="{Binding Path=Animals}" Grid.Row="2" AlternationCount="2"/>
<Button Content="New animal" Click="Button_Click"/>
</Grid>
</Window>
没有复制所有xaml代码,如Grid.RowDefinitions和Grid.ColumnDefinitions。在按钮上单击我只说newanimalwindown=newnewanimalwindow();n、 ShowDialog()代码>
NewAnimalWindow只有两个用于名称和重量的文本框,一个用于动物类型的组合框和一个按钮“Save animal”,我想在这个按钮上将动物添加到父窗口中的动物集合中。最简单的实现案例是在NewAnimalWindow中定义自定义构造函数并将AnimalList传递到那里:
public class NewAnimalWindow : ...
{
private List<Animal> _animals;
public NewAnimalWindow(List<Animal> animals)
{
_animals = animals;
}
....
public SaveButton_Click(...)
{
Animal animal = new Animal(name, weight);
_animals.Add(animal);
}
}
公共类NewAnimalWindow:。。。
{
私人动物名录;
公共新动物窗口(列出动物)
{
_动物=动物;
}
....
公共保存按钮\单击(…)
{
动物=新动物(名称、重量);
_动物。添加(动物);
}
}
其他选择包括:
在应用程序中某处使用全局动物列表
使用AddAnimal(animal)等方法发送一些回发界面,而不是动物列表,单击save按钮时将调用该方法
在NewAnimalWindow中创建一个事件,该事件将在单击“保存”按钮时触发
可能还有很多
我想在NewAnimalWindow上找到一个属性,它允许访问创建的对象
主窗口
var animalWindow = new NewAnimalWindow();
animalWindow.ShowDialog();
if (animalWindow.DialogResult.HasValue && animalWindow.DialogResult.Value)
{
var createdAnimal = animalWindow.CreatedAnimal;
Animals.Add(createdAnimal);
}
<Button IsDefault="True" Click="Button_OK_Click">Close</Button>
<Button IsCancel="True">Cancel</Button>
private Animal _animal;
public Animal CreatedAnimal
{
get
{
return _animal;
}
}
private void Button_OK_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
新建动物窗口
var animalWindow = new NewAnimalWindow();
animalWindow.ShowDialog();
if (animalWindow.DialogResult.HasValue && animalWindow.DialogResult.Value)
{
var createdAnimal = animalWindow.CreatedAnimal;
Animals.Add(createdAnimal);
}
<Button IsDefault="True" Click="Button_OK_Click">Close</Button>
<Button IsCancel="True">Cancel</Button>
private Animal _animal;
public Animal CreatedAnimal
{
get
{
return _animal;
}
}
private void Button_OK_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
关闭
取消
私人动物;
公共动物饲养场
{
收到
{
返回动物;
}
}
私有无效按钮\u确定\u单击(对象发送方,路由目标)
{
DialogResult=true;
}
或者,如果使用ViewModels,则可以在创建动物时使用中介模式和激发事件,以便主视图模型可以将其添加到列表中