【问题标题】:Databinding controls to properties of ComboBoxItem数据绑定控件到 ComboBoxItem 的属性
【发布时间】:2016-10-26 12:02:18
【问题描述】:

我正在尝试创建一个带有ComboBox 的WPF 窗口,您可以使用它选择一个项目,然后使用下面的TextBoxes 编辑当前选定项目的属性,例如NameAge

我如何使用数据绑定来做到这一点,即。将名称TextBox 绑定到ComboBox 中当前选定项的Name 属性?

我的 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>

我的代码在哪里

public partial class MainWindow : Window
{
    ObservableCollection<myObj> mo;


    public MainWindow()
    {
        InitializeComponent();

        mo = new ObservableCollection<myObj>();
        mo.Add(new myObj("Test1", 2));
        var ct  =  new MainWindowDatacontext(this);
        this.DataContext = ct;
        this.MyComboBox.SelectedIndex = 0;
    }

    private class MainWindowDatacontext : INotifyPropertyChanged
    {
        MainWindow parent;
        myObj curr;
        public MainWindowDatacontext(MainWindow mainWindow)
        {
            this.parent = mainWindow;
        }
        public ObservableCollection<myObj> MO
        {
            get
            {
                return parent.mo;
            }
        }
        public myObj CURR
        {
            get
            {
                return curr;
            }
            set
            {
                this.curr = value;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void executePropertyChanged(string s)
        {

            if(PropertyChanged!=null)
            {
                PropertyChanged(this.parent, new PropertyChangedEventArgs(s));
            }
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        mo.Add(new myObj("Test2", 10));
    }
}

但数据绑定仅适用于ComboBox-TextBoxes 永远不会绑定到任何东西。

myObj很简单:

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);
    }
}

【问题讨论】:

  • 您在调试过程中是否在输出控制台中收到错误消息?
  • 不 - 窗口加载或选择组合框中的元素时没有输出

标签: c# wpf xaml data-binding combobox


【解决方案1】:

因为您想绑定到应用程序中另一个元素的属性,您应该使用Binding.ElementName 属性。所以像这样改变你的TextBoxBinding

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

【讨论】:

    【解决方案2】:

    您在组合框中使用的是SelectedValue,而不是SelectedItem

    前者用于当您想从 ComboBox 的 ItemsSource 中的对象中选择一个属性而不是对象本身时。它与SelectedValuePath 结合使用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      相关资源
      最近更新 更多