简体   繁体   English

从Button更新文本框,单击C#

[英]Updating textbox from Button click C#

I have the following textbox 我有以下文本框

<TextBox Grid.Column="1" 
Grid.Row="1" 
Name="groupAddressBox"
Width ="80" 
Text="{Binding Path=GroupAddress,  Converter={StaticResource groupAddressConverter}}"/>

When I change the text manually, it's all good. 当我手动更改文本时,一切都很好。

But when I try to do this via a button 但是当我尝试通过按钮执行此操作时

private void Test_Click(object sender, RoutedEventArgs e)
{
    groupAddressBox.Text = "0/0/1";
}

Although the text changes, the source is not updated, and when I click on ok, it recognizes the value that was there before the change. 尽管文本更改了,但是源没有更新,当我单击“确定”时,它会识别出更改之前的值。 I cannot upgrade the source straight away, so I prefer to do this this way. 我无法立即升级源,因此我更喜欢这样做。

Is there something that can help me force the source upgrade via this way? 有什么可以帮助我通过这种方式强制进行源升级的东西吗?

Based on your question, I tried to create a Simple Example of MVVM Pattern with very basic functionality. 根据您的问题,我尝试创建具有基本功能的MVVM模式的简单示例。 Please do necessary change to XAML and CS file as I took the highlighted code only. 请仅对突出显示的代码进行XAML和CS文件的必要更改。

Helper Classes 助手类

public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
    }



public class CommandHandler : ICommand
    {
        public event EventHandler CanExecuteChanged { add { } remove { } }

        private Action<object> action;
        private bool canExecute;

        public CommandHandler(Action<object> action, bool canExecute)
        {
            this.action = action;
            this.canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return canExecute;
        }

        public void Execute(object parameter)
        {
            action(parameter);
        }
    }

ViewModel 视图模型

public class ViewModel : ViewModelBase
{ 
    private string groupAddress;
    public string GroupAddress
    {
        get
        {
            return groupAddress;
        }

        set
        {
            if(value != groupAddress)
            {
                groupAddress = value;
                OnPropertyChanged("GroupAddress");

            }
        }
    }

    public ViewModel() 
    { 

    } 

    private ICommand clickCommand; 
    public ICommand ClickCommand 
    { 
        get 
        { 
            return clickCommand ?? (clickCommand = new CommandHandler(() => MyAction(), true)); 
        } 
    } 

    public void MyAction() 
    { 
        GroupAddress = "New Group Address"; 
    } 
}

Window Xaml 窗口Xaml

<TextBox Grid.Column="1" Grid.Row="1" Width ="80" 
        Text="{Binding GroupAddress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

<Button Content="Push" Style="{StaticResource TransparentButtonStyle}"
             Margin="5" Command="{Binding ClickCommand}"/>

Window Xaml cs Window Xaml CS

ViewModel vm = new ViewModel();

this.DataContext = vm;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM