mardi 27 octobre 2015

How to dynamically link a CheckBox to enable a TextBox in C# (WPF)?

I am a total noob so please bear with me. I have a row in a grid with 5 textboxes, 2 of which are enabled by checkboxes. I am trying to dynamically add additional rows to the grid when a button is clicked. The eventhandler I added will only enable the textbox in the first row, but not in the current row (2nd). There is another eventhandler which handles the box in the first row, this is a new one. (BTW I only have part of the second row coded). Not sure if I should try making a template for the checkbox, and then use binding to the textbox? And if so, the instructions I've read on connecting the binding are vague and confusing. Or can I do the binding directly? Or ????

 public partial class Window2 : Window
{
    int currentColumn = 0;
    int currentRow = 1;
    int timesCalled = 1;
    public Window2()
    {
        InitializeComponent();
    }               
    private void AddLevelButton_Click(object sender, RoutedEventArgs e)
    {
        string level = this.Level.Content.ToString();  //label for the row
        string[] splitLevel = level.Split(' ');
        int levelNum = int.Parse(splitLevel[1]);
        levelNum = timesCalled + 1;            
        int nextRow = currentRow + 1;           
        int nextColumn = currentColumn + 1;
        Label levelLabel = new Label();
        levelLabel.Content = "Level " + levelNum.ToString();          
        Grid.SetRow(levelLabel, nextRow);
        Grid.SetColumn(levelLabel, currentColumn);
        FlowGrid.Children.Add(levelLabel);
        currentColumn++;
        CheckBox antesBox = new CheckBox();   //the checkbox to enable the
        antesBox.Name = "AntesBox";           //textbox which follows
        antesBox.VerticalAlignment = VerticalAlignment.Bottom;
        antesBox.HorizontalAlignment = HorizontalAlignment.Right;
        antesBox.FontSize = 16;
        antesBox.Width = 20;
        antesBox.Height = 20;
        antesBox.Checked += AntesBox_Checked1;      //eventhandler
        Grid.SetRow(antesBox, nextRow);
        Grid.SetColumn(antesBox, currentColumn);
        FlowGrid.Children.Add(antesBox);
        nextColumn = ++currentColumn;
        TextBox enterAntes = new TextBox();      //the textbox to be enabled
        enterAntes.Name = "EnterAntes";
        enterAntes.Margin = new Thickness(5, 0, 5, 0);
        enterAntes.FontSize = 16;
        enterAntes.FontFamily = new FontFamily("Verdana");
        enterAntes.IsEnabled = false;       
        enterAntes.KeyDown += EnterAntes_KeyDown1;    //tested; this works
        Grid.SetRow(EnterAntes, nextRow);
        Grid.SetColumn(EnterAntes, nextColumn);
        FlowGrid.Children.Add(EnterAntes);
        nextColumn = ++currentColumn;

    }

    private void enterAntes_KeyDown1(object sender, KeyEventArgs e)
    {
        int key = (int)e.Key;
        e.Handled = !(key >= 34 && key <= 43 ||
         key >= 74 && key <= 83 || key == 2); 
    }

    private void AntesBox_Checked1(object sender, RoutedEventArgs e)
    {
        EnterAntes.IsEnabled = true;
    }




Aucun commentaire:

Enregistrer un commentaire