2016-10-26 59 views
1

的性能數據綁定控件我想有一個ComboBox,與您選擇一個項目一個WPF窗口,然後使用下面TextBoxes編輯當前選擇的項目,如NameAge的性能。到ComboBoxItem

我該如何做到這一點與數據綁定,即。將名稱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); 
    } 
} 
+0

你在你的輸出控制檯收到錯誤調試過程中? – rbr94

+0

不,在窗口加載或當我選擇組合框中的元素時沒有輸出 – euanjt

回答

1

因爲您想綁定到應用程序中另一個元素的屬性,所以應該使用Binding.ElementName屬性。因此,改變你的TextBoxBinding這樣的:

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

你在你的組合框,而不是使用SelectedItemSelectedValue

前者適用於您希望從ComboBox的ItemsSource中的對象中選擇一個屬性而不是該對象本身。它與SelectedValuePath一起使用。

由於您要編輯所選項目的多個屬性,您需要使用SelectedItem