我在启动时有一个窗口,您可以从2个组合框中选择一个接口和端口。单击“完成”后,它将转到主窗口,您可以在其中看到另外两个相同的COMbox。它们具有您在弹出窗口中选择的选定值。我可以在后面的代码中执行此操作吗?
这是我的弹出窗口winResetInterface.xaml
:
Window x:Class="simpliphy.winResetInterface"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="winResetInterface" Height="280" Width="259">
<Grid Margin="0,-1,2,1">
<ComboBox Name="cmbInterface" ItemsSource="{Binding Interface}" DisplayMemberPath="Name" HorizontalAlignment="Left" Margin="94,52,0,0" VerticalAlignment="Top" Width="91"/>
<Button x:Name="btnFinishSetup" Click="btnFinishSetup_Click" Content="Done" HorizontalAlignment="Left" Margin="76,173,0,0" VerticalAlignment="Top" Width="75"/>
<ComboBox x:Name="cmbPort" ItemsSource="{Binding SelectedItem.Port, ElementName=cmbInterface}" HorizontalAlignment="Left" Margin="94,88,0,0" VerticalAlignment="Top" Width="91"/>
</Grid>
在我的代码背后:
namespace project
{
public partial class winResetInterface : Window
{
public ObservableCollection<Model.Device_Class> Interface { get; set; }
public winResetInterface()
{
InitializeComponent();
DataContext = this;
Interface = new ObservableCollection<Model.Device_Class>();
Interface.Add(new Device_Class() { Name = "XGBE", Port = new ObservableCollection<string>() { "0" } });
Interface.Add(new Device_Class() { Name = "SATA0", Port = new ObservableCollection<string>() { "0", "1", "2", "3" } });
}
}
private void btnFinishSetup_Click(object sender, RoutedEventArgs e)
{
MainWindow main = new MainWindow();
App.Current.MainWindow = main;
this.Close();
main.Show();
//Set the selected options on the device stats sidebar
///THIS IS WHERE I'M STUCK!
main.cmbInterface_main.SelectedItem = cmbInterface.SelectedItem;
}
}
在我的MainWindow中,我有一个组合框,其填充方式与box1,cmbInterface相同。我可以不只是将2个选定项目设置为彼此相等吗?
答案 0 :(得分:0)
你可以这样做:
main.cmbInterface_main.SelectedIndex = cmbInterface_main.FindStringExact(cmbInterface.Text, 0);
虽然循环代码的评论是一个更好的答案。