WPF将组合框的选定值发送到另一个窗口的相同的组合框

本文关键字:组合 另一个 窗口 WPF | 更新日期: 2023-09-27 18:03:20

我在启动时有一个窗口,您可以从2个组合框中选择接口和端口。当你点击Done时,它会将你带到主窗口,在那里你可以看到另外2个相同的comboxes。这些都是你在弹出窗口中选择的值。我可以在后面的代码中做到这一点吗?

这是我的弹出窗口,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;

        }
    }

在我的主窗口中,我有一个组合框,填充方式与box1, cmbInterface相同。我能不能让这两个选中的项相等呢?

WPF将组合框的选定值发送到另一个窗口的相同的组合框

你可以这样做:

main.cmbInterface_main.SelectedIndex = cmbInterface_main.FindStringExact(cmbInterface.Text, 0);