0

I'm working on an app which will sort numbers from .txt files and show them on interface. I have 2 text boxes, first one for loading all numbers from files before sorting and second one for sorted numbers but I can't get numbers from the first textbox. Help me please!

XAML

/// **this button loads files from system chosen by user and put numbers to first textblock** ///

<Button Name="load" Background="Pink" Grid.Column="0" BorderBrush="Black" BorderThickness="1" Click="load_Click" ClickMode="Press" Width="120" Height="30">
    Load File
</Button>


/// **First text block, numbers are loaded here but I can't load them back to viewModel to sort them :((**

<TextBox x:Name="numbers1" Text="{Binding NumbersString, 
                                  Mode=OneWayToSource, 
                                  UpdateSourceTrigger=PropertyChanged}"
                                  IsReadOnly="True" 
                                  TextWrapping="Wrap"
                                  FontSize="15"
                                  FontWeight="Bold"
                                  Width="Auto"
                                  Height="150"
                                  Margin="20,0,20,0"
                                  Background="AntiqueWhite"/>

/// I want to load numbers here, I don't have sort method yet but it doesn't matter now, tell me please how to put numbers from previous one to this one.

<TextBox x:Name="numbers2" Text="{Binding numbers, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                IsReadOnly="True" TextWrapping="Wrap"
                                                FontSize="15"
                                                FontWeight="Bold"
                                                Width="Auto"
                                                Height="150"
                                                Background="AntiqueWhite"/>

XAML.cs

I put event and property things here but I guess its bad.

public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            var evt = PropertyChanged;   // create local copy in case the reference is replaced
            if (evt != null)             // check if there are any subscribers
                evt(this, new PropertyChangedEventArgs(propertyName));
        }
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new numbersViewModel();
        }

        public void load_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
                numbers1.Text = numbers1.Text + " /// " + File.ReadAllText(openFileDialog.FileName);
                RaisePropertyChanged("numbers1");
        }
    }

ViewModel.cs

sealed class numbersViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private numbersModel _model;

        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler evt = PropertyChanged;   // create local copy in case the reference is replaced
            if (evt != null)             // check if there are any subscribers
                evt(this, new PropertyChangedEventArgs(propertyName));
        }

        public string NumbersString
        {
            get { return _model.numbersString; }
            set
            {
                if (value != _model.numbersString)
                {
                    _model.numbersString = value;
                    RaisePropertyChanged("numbers1");
                    RaisePropertyChanged("numbers2");
                }
            }
        }
        public string numbers2
        {
            get { return _model.numbersString2; }
            set
            {
                if (value != _model.numbersString2)
                {
                    _model.numbersString2 = value;
                    RaisePropertyChanged("numbers2");
                }
            }
        }

        public numbersViewModel()
        {
            _model = new numbersModel
            {
                numbersString = "",
                numbersString2 = ""

            };
        }

Model.cs

sealed class numbersModel : INotifyPropertyChanged
    {

        private string model="";
        private string model2 = "";
        public string numbersString { get { return model; } set { model = value; } }
        public string numbersString2 { get { return model2; } set { model2 = value; } }

        

        
        
    }

help please

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • 2
    _"i cant get numbers from the first textbox"_ -- in WPF, that should never be a thing you want to do. The numbers displayed in the first text box should be there by virtue of already being in a view model property bound to the text box. "Getting the numbers" should be a matter of retrieving them from that property, not retrieving them from the text box. Unfortunately, your question is too broad; you should study the MVVM pattern for WPF. If you still need help, improve this question so that it includes a [mcve], with _only_ the pertinent code (minimal) and _all_ the pertinent code (complete). – Peter Duniho Aug 24 '20 at 21:23
  • I agree it would be better to have some viewmodel logic to fill `NumbersString` and binding the property two-way. The problem with your approach is, that you first assign a binding to the `Text` property in xaml, then you assign a value to the `Text` property in code behind, basically removing the binding. Instead you need a way to change the property value without removing the binding. – grek40 Aug 25 '20 at 13:04
  • **Maybe** [using `SetCurrentValue` as this answer suggests](https://stackoverflow.com/a/4232379/5265292) can resolve your problem, but I'm not sure... – grek40 Aug 25 '20 at 13:05

0 Answers0