D
D
Dmitry Afonchenko2015-12-29 15:49:23
.NET
Dmitry Afonchenko, 2015-12-29 15:49:23

Why doesn't binding a result to the Content property of a button in WPF work?

Good afternoon, comrades, help me deal with the following question. There is such a form:
806c7adc3004484aa284acdb6d8fdd56
In theory, it should display the result of some function action in the Content of the button. For all fields, the following binding is made:

<TextBox Text="{Binding Path=DataModel.A0, UpdateSourceTrigger=PropertyChanged}" Name="dA0" Grid.Row="0" Margin="2,2,2,2" Grid.Column="1"/>
<TextBox Text="{Binding Path=DataModel.B0, UpdateSourceTrigger=PropertyChanged}" Name="dB0" Grid.Row="1" Margin="2,2,2,2" Grid.Column="1"/>
<TextBox Text="{Binding Path=DataModel.L, UpdateSourceTrigger=PropertyChanged}"  Name="dl" Grid.Row="2" Margin="2,2,2,2" Grid.Column="1"/>
<Button Command="{Binding DichotomyCmd}" Content="{Binding Path=DataModel.Ans, UpdateSourceTrigger=PropertyChanged}" Name="dBtn" Height="20" IsEnabled="True" Margin="2,2,2,2" Grid.Row="3" Grid.ColumnSpan="2"/>

public ICommand DichotomyCmd
        {
            get
            {
                if (_dichotomyCmd == null)
                    _dichotomyCmd = new RelayCommand(ExecuteDichotomyCommand, CanExecuteDichotomyCmd);
                return _dichotomyCmd;
            }
        }

        public void ExecuteDichotomyCommand(object parameter)
        {
            DataModel.Ans = Dichotomy(DataModel.A0, DataModel.B0, DataModel.L).ToString();
            DataModel = null;
        }

        public bool CanExecuteDichotomyCmd(object parameter)
        {
            if (DataModel.L == 0 && DataModel.N == 0)
                return false;
            else
                return true;
        }

class DataModel
    {
        public double A0 { get; set; }
        public double B0 { get; set; }
        public int N { get; set; }
        public double L { get; set; }
        public string Ans { get; set; }
        public string _L { get; set; }

        public DataModel()
        {
        }

        public DataModel(double a0, double b0, int n)
        {
            this.A0 = a0;
            this.B0 = b0;
            this.N = N;
        }

        public DataModel(double a0, double b0, float l)
        {
            this.A0 = a0;
            this.B0 = b0;
            this.L = l;
        }
    }

When you run it, you can see that the function calculates everything, but the result is not displayed. Why can this be so?
Just in case, the source code is here: https://github.com/Undermove/MethodsMax/tree/maste...
Thanks in advance for any information that will lead to a solution to the issue.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dasha Tsiklauri, 2015-12-29
@Indermove

1) DataModel inherit from INotifyPropertyChanged
2) why nullify DataModel = null; object reference is lost

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question