【问题标题】:I have bind numbers to CombBox. I want change value comboBox and textBox changed value我已将数字绑定到 ComboBox。我想更改值组合框和文本框更改值
【发布时间】:2013-04-07 23:05:19
【问题描述】:

我想从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 中的内容?

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

【问题讨论】:

  • 你有什么问题?
  • 如何从 CombBox 数字中更改 TextBox 中的内容?

标签: c# mvvm windows-8 combobox textbox


【解决方案1】:

我会更改您的视图模型代码,以便文本框绑定到在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}" ... />

【讨论】:

    【解决方案2】:

    先前的答案没有整合文本框内容必须在 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));
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 2021-08-17
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多