2013-10-11 33 views
0

我想綁定文本框到字符串變量同一時間我想要將ComboBox綁定到列表。同時綁定兩個控件到兩個源

<Window 
     x:Class="Assignment2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:validators="clr-namespace:Assignment2" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
    <Grid> 
     <ComboBox Height="23" HorizontalAlignment="Left" Margin="109,103,0,0" Name="StringComboBox" VerticalAlignment="Top" Width="120" SelectionChanged="SelectionChanged"> 
      <ComboBox.ItemsSource> 
       <Binding Path="ListString" Mode="TwoWay" UpdateSourceTrigger="LostFocus"></Binding> 
      </ComboBox.ItemsSource> 
     </ComboBox> 
     <TextBox Height="23" HorizontalAlignment="Right" Margin="0,51,250,0" Name="inputTextBox" VerticalAlignment="Top" Width="154" LostFocus="inputTextBox_LostFocus"> 
      <TextBox.Text> 
      <Binding Path="Name1" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"> 
       <Binding.ValidationRules> 
        <validators:RequiredFieldValidator ErrorMessage="This field Should not be empty"></validators:RequiredFieldValidator> 
        <validators:OnlyCharacterValidation ErrorMessage="Only characters allowed"></validators:OnlyCharacterValidation> 
       </Binding.ValidationRules> 
      </Binding> 
      </TextBox.Text> 
     </TextBox> 
    </Grid> 
</Window> 

代碼的背後是:

public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     string _name = "Default Value"; 
     public ObservableCollection<string> ListString; 
     public string Name1 
     { 
      get { return _name; } 
      set 
      { 
       _name = value; 
      } 
     } 
     public MainWindow() 
     { 
      ListString = new ObservableCollection<string>(); 
      ListString.Add("AAA"); 
      ListString.Add("BBB"); 
      ListString.Add("CCC"); 
      ListString.Add("DDD"); 

      InitializeComponent(); 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     private void inputTextBox_LostFocus(object sender, RoutedEventArgs e) 
     { 
      OnPropertyChanged("Name1"); 
     } 

    } 
} 

我似乎無法綁定組合框和文本框在同一時間。我已經嘗試了很多解決方案。但無論哪種方式只有這些作品之一。 有沒有簡單的解決方案呢?

回答

0
public string Name1 
    { 
     get { return _name; } 
     set 
     { 
      _name = value; 
     } 
    } 

忘記OnPropertyChanged("Name1");

另外:

public ObservableCollection<string> ListString; 

你ListString應該是一個屬性,而不僅僅是一個領域:

public ObservableCollection<string> ListString { get; set; } 

最後,我沒有看到這樣做的原因:

private void inputTextBox_LostFocus(object sender, RoutedEventArgs e) 
{ 
    OnPropertyChanged("Name1"); 
} 
1

你的房產應該打電話OnPropertyChanged這樣的處理程序:

public string Name1 
{ 
    get { return _name; } 
    set 
    { 
     _name = value; 
     OnPropertyChanged("Name1"); 
    } 
} 

private ObservableCollection<string> listString = new ObservableCollection<string>(); 

public ObservableCollection<string> ListString 
{ 
    get { return listString ; } 
    set 
    { 
     listString = value; 
     OnPropertyChanged("ListString"); 
    } 
} 

請仔細閱讀MSDN上INotifyPropertyChanged Interface頁面中的示例。

1

嘗試改變對此。

public string Name1 
{ 
    get { return _name; } 
    set 
    { 
     _name = value; 
     OnPropertyChanged("Name1"); 
    } 
} 
相關問題