Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wpf c#在一个窗口的标签中输入另一个窗口的值_C#_Wpf_Xaml_Label - Fatal编程技术网

wpf c#在一个窗口的标签中输入另一个窗口的值

wpf c#在一个窗口的标签中输入另一个窗口的值,c#,wpf,xaml,label,C#,Wpf,Xaml,Label,我在WPF C#有一个智能城市的gui 我有两个窗口窗体。第一个窗体中有一个组合框。第二个窗体中有一个标签。第一个窗体中有一个按钮,用于保存所选的组合框值并转到第二个窗体 单击按钮时的代码为: ComboBoxItem typeItem_a = (ComboBoxItem)comboBox_a.SelectedItem; string destination_a = typeItem_a.Content.ToString(); Window1 SmartPlan2 = new Wi

我在WPF C#有一个智能城市的gui 我有两个窗口窗体。第一个窗体中有一个组合框。第二个窗体中有一个标签。第一个窗体中有一个按钮,用于保存所选的组合框值并转到第二个窗体

单击按钮时的代码为:

ComboBoxItem typeItem_a = (ComboBoxItem)comboBox_a.SelectedItem;
string destination_a = typeItem_a.Content.ToString();       
Window1 SmartPlan2 = new Window1();
this.Hide();
SmartPlan2.ShowDialog(); // change window

现在,我想在第二个窗口中自动将我保存的字符串(字符串目的地_a)设置为标签。有什么想法吗?

@Ragavan是对的,您真的想尝试使用MVVM来实现它,下面是一个示例,说明如何实现您的目标:

Window1.xaml 你的切入点
您可以使用窗口的DataContext指定要使用的ViewModel,如果您为两者提供相同的ViewModel,它们将自动相互更新。

我强烈建议大家看看MVVM和Binding是如何与WPF一起工作的。

u必须使用相同的datacontext并绑定值,将其发送给Window1的构造函数,然后在其中将其放入标签中如何?
<Window x:Class="double_windiwed_mvvm.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:double_windiwed_mvvm"
    mc:Ignorable="d"
    Title="Window1" Height="300" Width="300">
<Grid>
    <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
              HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="10">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

</Grid>
<Window x:Class="double_windiwed_mvvm.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:double_windiwed_mvvm"
    mc:Ignorable="d"
    Title="Window2" Height="300" Width="300">
<Grid>
    <Label Content="{Binding SelectedItem.Name}"
           HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="10"/>
</Grid>
class ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Person> Items { get; set; } = new ObservableCollection<Person>();

    private Person _selectedItem;
    public Person SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            if(_selectedItem != null && _selectedItem.Equals(value)) { return; }
            _selectedItem = value;
            PropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
        }
    }

    // INotifyPropertyChanged emplementation
    public event PropertyChangedEventHandler PropertyChanged;
}
class Person
{
    public string Name { get; set; }
}
ViewModel vm = new ViewModel();
vm.Items.Add(new Person { Name = "Bob" });
vm.Items.Add(new Person { Name = "Marie" });
vm.Items.Add(new Person { Name = "John" });

Window1 w1 = new Window1();
w1.DataContext = vm;

Window2 w2 = new Window2();
w2.DataContext = vm;

w1.Show();
w2.Show();