这是名为“_combo”的组合框的 xaml:
<ComboBox SelectedValue="{Binding SelectedValue}" x:Name="_combo" >
      <ComboBoxItem Content="One"/>
      <ComboBoxItem Content="Two"/>
      <ComboBoxItem Content="Three"/>
</ComboBox>
这是背后的代码:
private string _SelectedItem;
public string SelectedItem
{
    get { return _SelectedItem; }
    set
    {
        _SelectedItem = value;
        OnPropertyChanged("SelectedItem");
        textbox5.text = _combo.Content.ToString();
    }
}
如果这让您感到困惑,请查看数据绑定和“Inotifypropertychanged”。
编辑:这是我输入的简短示例中的所有代码。
基本上,您有一个名为 MainViewModel 的对象,并且您的 XAML 中的所有属性都绑定到视图模型对象属性。下面,TextBox 有一个名为 text 的属性,默认为“ComboBox binding example -- ...”,因为这是视图模型中的 string 属性所绑定的。如果更改框中的文本,则后面代码中的属性值会更改。如果您在后面的代码中编写的某些代码更改了属性,则文本框也会在 XAML 中更新。这是在 WPF 中开发的最佳方式!(或至少大部分时间)。我花了很长时间才理解它是如何工作的,但是一旦我明白了它,开发就变得更快更容易了。最重要的是,您可以完全改变您的 GUI 外观,而执行所有操作的代码保持不变。
这也可以通过向 ComboBox 添加选择更改事件来完成。在这种情况下它实际上更容易,但没有提供 INotifyPropertyChanged 所做的功率量。即因为事件只在一个方向触发,即 ComboBox_Changed -> 事件在代码中触发。INotifyPropertyChanged 在这两个方向上都有效。
这种编码风格被称为 MVVM。虽然它经常让初学者感到困惑,但一旦你把它搞定,它就是一种很棒的应用程序方式。
<Window x:Class="ComboBoxSample.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" x:Name="view">
<Grid>
    <StackPanel>
        <TextBox x:Name="_textbox" Margin="5,5,5,5" Text="{Binding DataContext.text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
        <ComboBox x:Name="_combo"  Margin="5,0,5,5" SelectedItem="{Binding DataContext.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
            <ComboBoxItem Content="Blue"/>
            <ComboBoxItem Content="Red"/>
            <ComboBoxItem Content="Green"/>
            <ComboBoxItem Content="Black"/>
        </ComboBox>
        <TextBox x:Name="_textbox2" Text="Other method for updating text" Margin="5,5,5,5"/>
        <ComboBox x:Name="_combo2" SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem Content="One"/>
            <ComboBoxItem Content="Two"/>
            <ComboBoxItem Content="Three"/>
        </ComboBox>
    </StackPanel>
</Grid>
这是mainwindow.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ComboBoxSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel(_combo);
    }
    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string value = _combo2.Items[_combo2.SelectedIndex].ToString().Substring(38);
        _textbox2.Text = value;
    }
}
}
这是所有重要的视图模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ComboBoxSample
{
public class MainViewModel : INotifyPropertyChanged
{
    public ComboBox combo;
    public TextBox textbox;
    public MainViewModel(ComboBox _combo)
    {
        combo = _combo;
    }
    private string _text = "ComboBox Binding Example --Changing the combox will change this text!";
    public string text
    {
        get { return _text; }
        set
        {
            _text = value;
            OnPropertyChanged("text");
        }
    }
    private string _SelectedItem;
    public string SelectedItem
    {
        get { return _SelectedItem; }
        set
        {
            _SelectedItem = value;
            OnPropertyChanged("SelectedValue");
            string val = combo.Items[combo.SelectedIndex].ToString().Substring(38);
            text = val;
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
}