binding for the text control - WPF - C#

Markus Freitag 3,786 Reputation points
2020-07-13T13:57:48.877+00:00

Hello,

How I can make a binding for the text control?
I use as DataContext ViewModel.

  public class ViewModel : INotifyPropertyChanged  
    {  
        private CollectionViewSource cvs1 = new CollectionViewSource();  
        private ObservableCollection<Order> ColOrders;  
        public ICollectionView View1 { get => cvs1.View; }  
        private CollectionViewSource cvs2 = new CollectionViewSource();  
        private ObservableCollection<Material> col2;  
        public ICollectionView View2 { get => cvs2.View; }  
        public ICommand Cmd { get => new RelayCommand(CmdExec, CanCmdExec); }  
  
        //public ViewModel()  
        //{  
        //    CurrentUIData = new UserData();  
        //}  
        public string CurrentOrderNo { get; set; }  

<Window.Resources>  
    <local:ViewModel x:Key="vm"/>  

11908-binden.png

11909-binden2.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Markus Freitag 3,786 Reputation points
    2020-07-13T18:35:34.817+00:00

    Where is your problem?

    Hi Peter, big problem!

            private void CmdExec(object obj)
            {
                switch (obj.ToString())
                { 
                    case "AddOrder":
                        //nt newOrderNumber = (this.ColOrders.Count > 0) ? this.ColOrders.Max((ord) => ord.OrderNumber) + 1 : 1;
                        var o = new Order() { OrderNumber = "TEST1" };
                        this.ColOrders.Add(o);
                        this.View1.MoveCurrentTo(o);
    

    We have the TextBox Order. The operator insert this number now as string.
    How can I access the UI controls directly from ViewModel?
    And I need like a Init() function. How can I do that?
    I want not to press first Load()

            <TextBox Grid.Row="0" Grid.Column="0" Text="{Binding CurrentOrderNo}"  HorizontalAlignment="Left" 
    

    Works not.

    The goal:
    var o = new Order() { OrderNumber = textBox der Usereingabe };

    Can you help me again, please?

    new ObservableCollection<Order>();
    Init to create a new list.

    How I can call a Init() function for ViewModel ?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.