N
N
nicklayk2014-01-15 13:28:27
Windows phone
nicklayk, 2014-01-15 13:28:27

How to block form buttons in a cycle in WP?

There is a form, there is a variable, you need to block buttons in the loop depending on the value of the variable.
Approximate representation:

for(int i=0;i<value;i++) {
    btn_[i].IsEnabled = false;
}

How to implement?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vadim Martynov, 2014-01-15
@nicklayk

Oh. Let's start.
1. Do I understand correctly that buttons are generated dynamically? Or is the btn_[i] collection filled in advance and does it contain all the buttons?
2. Can I learn more about the scenario in which this task occurred? Now it is not clear at what point in time the button availability should change.
3. ViewFirst or ViewModelFirst are you using?
In any case, if such a situation arises, then the MVVM pattern is no longer respected - the view controls itself, and not the data controls the view. There are several options here.
If using commands does not suit you , then in any case, somewhere there must be the same variable with a value that you can bind to.
If the variable is not a boolean type, then this can be done through the converter .
If the buttons are generated in the container of elements, depending on the content of the collection in the VM, then you can bind to the collection element. If the collection needs to be bound to an external property, then using RelativeSource will do .
If, nevertheless, there is a variable in the View that affects the availability of buttons, it is worth considering how to change the code so that such a scenario does not occur. Compliance with mvvm is not always easy, but trying to do so is highly recommended. No matter what they write on Habré, but this is a convenient template that allows you not to get confused in the code and develop as quickly as possible.
I would still ask to describe the problem in more detail, now I don’t even understand what exactly the snag arose.

V
Vyacheslav Zolotov, 2014-01-15
@SZolotov

Not the best idea. Do better binding in XAML

G
gleb_kudr, 2014-01-15
@gleb_kudr

Well, if you don’t want to do it through bindings, then put all the buttons in the list and block them by index. A bit of pseudocode.

//Наполняем массив
List<Button> myButtons=new List<Button>();
for(int i=1;i<10;i++){
 var newButton= new Button();
 form1.add(newButton);
 myButtons.add(newButton);
}
//вырубаем кнопки
foreach(Button btn in myButtons){
 if(btn!=null){
  btn.IsEnabled=false;
 }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question