以编程方式从ItemControl WPF中删除TextBox

时间:2015-07-28 20:16:13

标签: c# wpf textbox

我试图删除文本框,仅用于学习目的。我已经搜索过该主题,但是我遇到了一个问题,因为我在ItemsControl中遇到阻止我删除的文本框问题。我可以添加一个文本框,但是我无法将其删除。

<ItemsControl x:Name="QueueControl" ItemsSource="{Binding}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

生成控件的代码

public void GenerateControls()
{
    StackPanel mainStackPanel = new StackPanel();
    mainStackPanel.Name = "MainStackPanel";
    mainStackPanel.SetValue(FrameworkElement.NameProperty, "MainStackPanel");
    for (int i = 0; i < queuelist.Count; i++)
    {
        StackPanel tempStackPanel = new StackPanel();
        tempStackPanel.Orientation = Orientation.Horizontal;
        tempStackPanel.Margin = new Thickness(0, 5, 0, 5);

        Label tempLabel = new Label();
        tempLabel.FontFamily = new FontFamily("Arial");
        tempLabel.FontWeight = FontWeights.Bold;
        tempLabel.FontSize = 10.25;
        tempLabel.Foreground = (Brush)(new System.Windows.Media.BrushConverter().ConvertFromString("#DFE0E6"));
        tempLabel.Content = queuelist[i].queueName;
        tempStackPanel.Children.Add(tempLabel);

        TextBox tempTextbox = new TextBox();
        tempTextbox.Width = 40;
        tempTextbox.Text = queuelist[i].queuePercent;
        tempTextbox.IsEnabled = false;
        tempTextbox.Style = Application.Current.FindResource("RoundedTextBox") as Style;
        tempTextbox.Margin = new Thickness(5, 0, 5, 0);
        tempTextbox.KeyDown += queueTextBox_KeyPress;
        tempTextbox.SetValue(FrameworkElement.NameProperty, String.Format("queueTextBox{0}", i));
        tempStackPanel.Children.Add(tempTextbox);

        string tempPercent = String.Empty;
        if(i==0)
        {
            tempPercent = "100";
        }
        else
        {
            tempPercent = queuelist[i - 1].queuePercent;
        }

        Label tempLabel2 = new Label();
        tempLabel2.FontFamily = new FontFamily("Arial");
        tempLabel2.FontWeight = FontWeights.Bold;
        tempLabel2.FontSize = 10.25;
        tempLabel2.Foreground = (Brush)(new System.Windows.Media.BrushConverter().ConvertFromString("#DFE0E6"));
        tempLabel2.Content = String.Format("to {0}%", tempPercent);
        tempStackPanel.Children.Add(tempLabel2);
        mainStackPanel.Children.Add(tempStackPanel);
    }
    QueueControl.Items.Add(mainStackPanel);
}

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
{
    // Confirm parent and childName are valid. 
    if (parent == null) return null;

    T foundChild = null;

    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        // If the child is not of the request child type child
        T childType = child as T;
        if (childType == null)
        {
            // recursively drill down the tree
            foundChild = FindChild<T>(child, childName);

            // If the child is found, break so we do not overwrite the found child. 
            if (foundChild != null) break;
        }
        else if (!string.IsNullOrEmpty(childName))
        {
            var frameworkElement = child as FrameworkElement;
            // If the child's name is set for search
            if (frameworkElement != null && frameworkElement.Name == childName)
            {
                // if the child's name is of the request name
                foundChild = (T)child;
                break;
            }
        }
        else
        {
            // child element found.
            foundChild = (T)child;
            break;
        }
    }

    return foundChild;
}

我尝试过的事情:

private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
    var allTextBoxes = FindVisualChildren<TextBox>(this);
    List<TextBox> tbList = new List<TextBox>();
    StackPanel test = (StackPanel)this.FindName("MainStackPanel");
    foreach (TextBox tb in allTextBoxes)
    {
        tbList.Add(tb);
    }
    test.Children.Remove(tbList[tbList.Count - 1]);
}

private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
    var allTextBoxes = FindVisualChildren<TextBox>(this);
    List<TextBox> tbList = new List<TextBox>();
    foreach (TextBox tb in allTextBoxes)
    {
        tbList.Add(tb);
    }
    QueueControl.Items.Remove(tbList[tbList.Count - 1]);
}

0 个答案:

没有答案