-2

我想更改数字ComboBox并更改TextBox.

(例如,我有数字 = 2,内容位于“lblblblb” ofc。这是在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 中的内容?

读取视图模型.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;    
    }
}
4

2 回答 2

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}" ... />
于 2013-04-15T21:58:47.080 回答
0

先前的答案没有整合文本框内容必须在 TwoWays 中的事实,因此,在这种情况下,您可以将您的属性与 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));
        }
    }

}
于 2013-04-15T22:46:09.740 回答