C# 将控件数据绑定到ComboBoxItem的属性

C# 将控件数据绑定到ComboBoxItem的属性,c#,wpf,xaml,data-binding,combobox,C#,Wpf,Xaml,Data Binding,Combobox,我正在尝试创建一个带有组合框的WPF窗口,您可以使用组合框选择一个项目,然后使用下面的文本框来编辑当前所选项目的属性,例如Name和Age 如何使用数据绑定,即将名称文本框绑定到组合框中当前选定项的名称属性 我的XAML是 <Window x:Class="BindingTest001.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="

我正在尝试创建一个带有组合框的WPF窗口,您可以使用组合框选择一个项目,然后使用下面的
文本框来编辑当前所选项目的属性,例如
Name
Age

如何使用数据绑定,即将名称
文本框
绑定到
组合框
中当前选定项的
名称
属性

我的XAML是

<Window x:Class="BindingTest001.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        >
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="10,75,0,0" TextWrapping="Wrap" Text="{Binding Path=CURR.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="497"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="10,103,0,0" TextWrapping="Wrap" Text="{Binding Path=CURR.Age, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
        <ComboBox ItemsSource="{Binding Path=MO}" SelectedValue="{Binding Path=CURR, Mode=TwoWay}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="497" Name="MyComboBox"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="322,181,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>
您正在组合框中使用,而不是

前者用于从组合框的ItemsSource中的对象中选择一个属性,而不是对象本身。它与


当您想要编辑所选项目的多个属性时,您需要使用
SelectedItem

,因为您想要绑定到应用程序中另一个元素的属性,所以您应该使用属性。因此,将您的
文本框的
绑定更改如下:

Text="{Binding SelectedItem.Name, ElementName=MyComboBox, 
               Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Text="{Binding SelectedItem.Age, ElementName=MyComboBox,
               Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

调试期间,您的输出控制台是否收到错误?否-在窗口加载或在组合框中选择元素时没有输出
public class myObj : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    public void propertyChanged(string s)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(s));
        }
    }


    string name;

    public string Name
    {
        get { return name; }
        set {
            name = value;
            propertyChanged("Name");
        }
    }
    int age;


    public myObj(string p1, int p2)
    {
        this.name = p1;
        this.age = p2;
    }

    public int Age
    {
        get { return age; }
        set { age = value;
        propertyChanged("Age");
        }
    }

    public override string ToString()
    {
        return String.Format("{0}, {1}", name, age);
    }
}
Text="{Binding SelectedItem.Name, ElementName=MyComboBox, 
               Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Text="{Binding SelectedItem.Age, ElementName=MyComboBox,
               Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />