0

我需要帮助捕捉错误。下面是提供的代码。

这是表单的控件。当用户关闭下拉菜单时,表单将自动添加到另一行:

        private void addNewProfRow(object sender, EventArgs e) //all will invoke this generic method to display the visibility of the next
    {
        ComboBox selectedTest = (ComboBox)sender;
        Canvas thisRow = (Canvas)selectedTest.Parent;
        int index = Int32.Parse(thisRow.Name.Substring(thisRow.Name.Length - 1, 1)); //gets the num at the end of the name

        if (thisRow is Canvas && index == profMultipleCount && index < 9) //needs to ensure that only the previous canvas can invoke the next canvas once selection is done
        {
            profMultipleCount++; //points to the next row of canvas
            Canvas newRow = (Canvas)this.FindName("profileComp" + profMultipleCount); //finds the object and cast as Canvas
            newRow.Visibility = Visibility.Visible; //makes it visible

        }
        //hides the textBox in the case that user added a test component he did not want
        if (thisRow is Canvas && selectedTest.Text == "")
        {
            Canvas newRow = (Canvas)this.FindName("profileComp" + profMultipleCount);
            testComp = (ComboBox)this.FindName("testComp" + profMultipleCount);
            display = (CheckBox)this.FindName("profDisplay" + profMultipleCount);
            testComp.Text = "";
            display.IsChecked = false;
            newRow.Visibility = Visibility.Hidden;
            profMultipleCount--;
        }
    }

这是保存用户输入的方法。

        private void saveProfile(object sender, RoutedEventArgs e)
    {
        int i = 1;
        Canvas newRow = (Canvas)this.FindName("profileComp" + i);
        testComp = (ComboBox)this.FindName("testComp" + i);
        while (testComp.Text!= "" && i < 10 && newRow.Visibility == Visibility.Visible) //order matters becoz newRow depends on i
        {
            //input the rowItem in the format (profileCode, testComp, displayable, action) of the excel sheet
            display = (CheckBox)this.FindName("profDisplay" + i);
            testComp = (ComboBox)this.FindName("testComp" + i);
            userData.setMulRowData(profileTab.Tag.ToString() + "," + (profileCodeCB.Text + "," + testComp.Text + "," + display.IsChecked.Value.ToString()[0]));
            Console.WriteLine(i + "," + (profileCodeCB.Text + "," + testComp.Text + "," + display.IsChecked.Value.ToString()[0]));
            i++; profileIndex++;
            newRow = (Canvas)this.FindName("profileComp" + i);
        }
        profileIndex++;
        clearProfile(sender, e);
    }

我的 xaml 在这里:

<Grid Background="White" Height="3200" VerticalAlignment="Top">
        <Grid x:Name="panelTab" Height="410" Tag="Panel Component" VerticalAlignment="Top" Margin="10,0" IsEnabled="False">
            <Label Content="Panel Component:" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="None" FontSize="16" FontFamily="Segoe UI Symbol" Width="167" FontWeight="Bold"/>
            <Label Content="Panel Code:" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold" Margin="10,50,0,0"/>
            <ComboBox x:Name="panelCodeCB" HorizontalAlignment="Left" Margin="134,54,0,0" VerticalAlignment="Top" Width="85" IsEditable="True" Tag="Panel Code"/>
            <StackPanel x:Name="testCompList" HorizontalAlignment="Left" Height="274" Margin="10,92,0,0" VerticalAlignment="Top" Width="361">
                <Canvas x:Name="comp1" Margin="0,0,8,0" Height="26">
                    <Label Content="Panel Component 1*:" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold"/>
                    <ComboBox x:Name="pComp1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Panel Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewPRow"/>
                </Canvas>
                <Canvas x:Name="comp2" Margin="0,0,8,0" Height="26" Visibility="Hidden">
                    <Label Content="Panel Component 2:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                    <ComboBox x:Name="pComp2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Panel Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewPRow"/>
                </Canvas>
            </StackPanel>
            <TextBlock HorizontalAlignment="Left" Margin="496,0,0,0" VerticalAlignment="Top"><Hyperlink NavigateUri="C:\Users\Documents\Visual Studio 2012\Projects\User Upload\Data Upload\MainPanel.xaml"><Run Text="Add New..."/></Hyperlink></TextBlock>
            <Button Name="panelBtn" Content="Save and Add Another" HorizontalAlignment="Left" Margin="418,344,0,0" VerticalAlignment="Top" Width="135" Click="savePanel"/>
        </Grid>

当我为用户输入做一个 writeLine 时,我得到的是:

1,tth,1 2,tth,2 3,tth,3 4,tth,

但用户实际输入的是:

1,tth,1 2,tth,2 3,tth,3

所以我想问我的while循环为什么不限制空行并且仍然允许它进入?

编辑:当用户没有关闭最后一个下拉框时,我得到用户实际输入的内容,这就是我想要的,但我不知道为什么会这样。

4

1 回答 1

0

最好使用像 MVVM 这样的模式重新开始。有许多教程和书籍,但我会从一开始就指出。

Chris Mosers 有一个很好的WPF 教程网站,从他对 ​​MVVM 的解释开始。

它将与您习惯的不同,但最终您将摆脱所显示的代码。不,它不是“简单的实现方式”,但如果它是简单或好的问题,那就永远好。

于 2013-04-24T06:25:23.223 回答