2016-07-14 10 views
1

-Code in Zusammenhangchange 'IsEnabled' für WPF texbox in Benutzersteuerelements (Code-Behind)

private void button2_Click(object sender, RoutedEventArgs e) 
    { 
     edit(); 
    } 

    public void edit() 
    { 

     textBox1.IsEnabled = true; 
     textBox2.IsEnabled = true; 
     textBox3.IsEnabled = true; 
     textBox4.IsEnabled = true; 
     textBox5.IsEnabled = true; 
     textBox6.IsEnabled = true; 
     textBox7.IsEnabled = true;    
     textBox8.IsEnabled = true; 
     textBox9.IsEnabled = true; 
     textBox10.IsEnabled = true; 
     textBox11.IsEnabled = true; 
     textBox12.IsEnabled = true; 
     textBox13.IsEnabled = true; 
     textBox14.IsEnabled = true; 
     textBox15.IsEnabled = true; 
     textBox16.IsEnabled = true; 
     textBox17.IsEnabled = true; 
     textBox18.IsEnabled = true; 
    } 

I durchführen möchte, dass die eine einfache for-Schleife oben verwenden, das durch 1-18-Schleifen.

Ich habe die followng Methode versucht, aber nicht wie

bestimmt
for(i=0;i<19;i++) 
    { 
      textBox"" + i + "".IsVisible = true; 
    } 

Ich bin neu in WPF, und ich bin die Migration meiner App von WinForms zu WPF.

+0

Wäre es in WinForms arbeiten? Ich sehe eine seltsame Syntax 'textBox" "+ i +" ".IsVisible = true;' – Alex

Antwort

3

Verwendung verbindlich.

XAML (MyUserControl):

<UserControl Name="MyControl" ... 
.... 

    <TextBox Name="textBox1" IsEnabled="{Binding ElementName=MyControl, Path=AreTextBoxesEnabled}" ... /> 
    <TextBox Name="textBox2" IsEnabled="{Binding ElementName=MyControl, Path=AreTextBoxesEnabled}" ... /> 
    <TextBox Name="textBox3" IsEnabled="{Binding ElementName=MyControl, Path=AreTextBoxesEnabled}" ... /> 
... 

-Code-behind (MyUserControl):

public static readonly DependencyProperty AreTextBoxesEnabledProperty = DependencyProperty.Register(
    "AreTextBoxesEnabled", 
    typeof(bool), 
    typeof(MyUserControl)); 

public bool AreTextBoxesEnabled 
{ 
    get { return (bool)GetValue(AreTextBoxesEnabledProperty); } 
    set { SetValue(AreTextBoxesEnabledProperty, value); } 
} 

Gerade AreTextBoxesEnabled = true; Aufruf werden alle Textfelder aktiviert werden.

Natürlich gibt es viele andere Möglichkeiten. Aber dies ist der grundlegende Weg (ohne MVVM), dies zu tun, indem die Kraft der Bindung genutzt wird.

Einfache Lösung (aber nicht empfohlen) Weg ist so einfach wie:

for (i = 0; i < 19; i++) 
{ 
    var tb = this.FindName("textBox" + i.ToString()) as TextBox; 
    if (tb != null) tb.IsEnabled = true; 
} 
+0

Vereinbart, Bindung ist eine natürlichere Lösung für WPF – Alex

+1

"DataContext" in einem UserControl ändern ist eine schlechte Idee. 'DataContext' sind Benutzerdaten. viel besser verwenden RelativeSource oder ElementName für IsEnabled Bindung oder TemplateBinding in ControlTemplate – ASh

+0

@ASh Einverstanden, ich hatte dieses Problem vor haha. – Jai

0

Erstellen Sie eine Liste von Textfeldern wie:

var textBoxes = new List<TextBox>(); 

// Btw, ich habe nicht einen Compiler von Hand, gehe ich davon aus der Typ TextBox ist.

Fill Textfelder:

textBoxes.Add(textBox1); 
textBoxes.Add(textBox2); 
... 
textBoxes.Add(textBox18); 

Dies ist eine einmalige manuelle Aktion um ihn zu füllen. Danach können Sie eine Schleife durch sie:

foreach (var textBox in textBoxes) 
{ 
    textBox.IsVisible = true; 
} 

Oder verwenden Sie eine andere Einstellung/Algorithmus auf die Textfelder mit der foreach-Schleife (oder, Linq usw.).