条件检查组合框

时间:2013-04-24 02:46:35

标签: c# wpf

我需要帮助抓错。以下是提供的代码。

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

        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,第t个,1 2,Tth时,2 3,Tth时,3 4,Tth时,

但用户实际键入的是:

1,第t个,1 2,Tth时,2 3,Tth时,3

所以我想问一下我的while循环如何不限制空行并仍允许它进入?

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

1 个答案:

答案 0 :(得分:0)

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

克里斯·莫泽斯(Chris Mosers)以WPF tutorialsite开始explanation of MVVM开场{。}}。

它将与您习惯的不同,但最终您将被释放您所显示的代码。并非它不是一种“简单的实施方式”,但如果它是一个简单或好的问题,那么它就会好起来。