2013-04-15 20 views
-2

我想從ComboBox更改編號並更改TextBox中的值。我已將數字綁定到CombBox。我想要更改值組合框和文本框更改值

(例如,我有在 「lblblblb」 OFC數= 2和內容。這是ObservableCollection<string>,所以我打電話ContentWithListView[SelectNumberStep]

ReadPage.xaml

<TextBox HorizontalAlignment="Left" Margin="580,154,0,0" 
TextWrapping="Wrap" Text="{Binding ContentWithListView[SelectNumberStep],Mode=TwoWay}" 
VerticalAlignment="Top" Width="725" Height="82"/> 

<ComboBox HorizontalAlignment="Left" Margin="440,154,0,0" 
ItemsSource="{Binding NumberStep,Mode=TwoWay}" 
SelectedItem="{Binding SelectNumberStep,Mode=TwoWay}" 
VerticalAlignment="Top" Width="95" Height="77" /> 

如何改變來自CombBox數字的TextBox內容?

ReadViewModel.cs

private ObservableCollection<string> contentWithListView; 
public ObservableCollection<string> ContentWithListView 
{ 
    get 
    { 
     return this.contentWithListView; 
    } 

    set 
    { 
     this.contentWithListView = value; 
    } 
} 

private ObservableCollection<int> stepNumber; 
public ObservableCollection<int> NumberStep 
{ 
    get 
    { 
     return this.stepNumber; 
    } 

    set 
    { 
     this.stepNumber = value; 
    } 
} 

private int selectNumberStep; 
public int SelectNumberStep 
{ 
    get 
    { 
     return this.selectNumberStep; 
    } 

    set 
    { 
     this.selectNumberStep = value;  
    } 
} 
+1

什麼是你的問題? –

+0

如何從CombBox數字更改TextBox中的內容? – MichaelS

回答

0

以前的答案沒有整合他的文本框內容必須在雙向的事實,所以在這種情況下,您可以將您的屬性與INotifyPropertyChanged界面合併到一起:

的XAML部分

<StackPanel d:DataContext="{d:DesignInstance Type=classes:StackOverFlowX }"> 

    <TextBox Text="{Binding Content, Mode=TwoWay}"/> 

    <ComboBox ItemsSource="{Binding NumberStep, Mode=TwoWay}" 
       SelectedItem="{Binding SelectNumberStep,Mode=TwoWay}"/> 

</StackPanel> 

using System.ComponentModel; 
using System.Runtime.CompilerServices; 

public class StackOverFlowX : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    public StackOverFlowX() 
    { 

    } 

    private ObservableCollection<string> contentWithListView; 
    public ObservableCollection<string> ContentWithListView 
    { 
     get 
     { 
      return this.contentWithListView; 
     } 

     set 
     { 
      this.contentWithListView = value; 
      OnPropertyChanged(); 
     } 
    } 

    private ObservableCollection<int> stepNumber; 
    public ObservableCollection<int> NumberStep 
    { 
     get 
     { 
      return this.stepNumber; 
     } 
     set 
     { 
      this.stepNumber = value; 
      OnPropertyChanged(); 
     } 
    } 

    private int selectNumberStep; 
    public int SelectNumberStep 
    { 
     get 
     { 
      return this.selectNumberStep; 
     } 
     set 
     { 
      this.selectNumberStep = value; 
      OnPropertyChanged(); 
      OnPropertyChanged("Content"); 
     } 
    } 

    private string _content; 
    public string Content 
    { 
     get 
     { 
      return contentWithListView[this.SelectNumberStep]; 
     } 
     set 
     { 
      this._content = value; 

      if (contentWithListView.IndexOf(value) > -1) 
      { 
       SelectNumberStep = contentWithListView.IndexOf(value); 
       OnPropertyChanged("SelectNumberStep"); 
      } 

      OnPropertyChanged(); 

     } 
    } 

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var eventHandler = this.PropertyChanged; 
     if (eventHandler != null) 
     { 
      eventHandler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

} 
+0

thx!就是這個 ! ;) – MichaelS

0

我會改變周圍您的視圖模型的代碼,使文本框綁定到一個標量字符串值,這是對SelectNumberStep變化更新:

public string Content 
{ 
    get 
    { 
     // bounds checking here.. 
     return contentWithListView[this.SelectNumberStep]; 
    } 
} 

public int SelectNumberStep 
{ 
    get 
    { 
     return this.selectNumberStep; 
    } 

    set 
    { 
     this.selectNumberStep = value; 
     this.NotifyOfPropertyChange(() => this.SelectNumberStep); 
     this.NotifyOfPropertyChange(() => this.Content); 
    } 
} 

<TextBox Text="{Binding Content}" ... /> 
+0

好方法:)。謝謝! – MichaelS