在WPF中动态添加文本框

时间:2014-06-03 05:50:58

标签: c# wpf textbox

我正在动态创建一个文本框。我的网格中有2列。如果另一个文本框值=" tea",我想在行中添加新的文本框。我想创建新的文本框到相应的行文本框值更改。我无法使用Tag在此处获取所选行。因为我已经出于某种目的使用了Tag。我不太了解Tag。无论如何,如何将新文本框添加到column1到相应的行? 这是我的代码..

    public int count = 1;
    public TextBox txt1;

    private void btn_addnew_Click(object sender, RoutedEventArgs e)
    {

        //Creating Rows..
        RowDefinition row0 = new RowDefinition();
        row0.Height = new GridLength(40);
        grid1.RowDefinitions.Add(row0);

        //Creating columns..
        ColumnDefinition col0 = new ColumnDefinition();
        ColumnDefinition col1 = new ColumnDefinition();

        col0.Width = new GridLength(150);
        col1.Width = new GridLength(250);

        grid1.ColumnDefinitions.Add(col0);
        grid1.ColumnDefinitions.Add(col1);

        int i = count;

        //1st Column TextBox

        txt1 = new TextBox();
        txt1.Margin = new Thickness(10, 10, 0, 0);
        Grid.SetRow(txt1, i);
        Grid.SetColumn(txt1, 0);

        txt1.Tag = txt1;

        txt1.MouseEnter+=txt1_MouseEnter;
        txt1.TextChanged += txt1_TextChanged;
        grid1.Children.Add(txt1);
        count++;
        }

    private void txt1_MouseEnter(object sender, MouseEventArgs e)
    {
        txt1 = ((TextBox)sender).Tag as TextBox;
        popup.IsOpen = true;
    }

    public TextBox txt2;
    private void txt1_TextChanged(object sender, TextChangedEventArgs e)
    {


        if (txt1.Text.ToString() == "Tea")
        {

            txt2 = new TextBox();

         //How to set row here?

            Grid.SetRow(txt2, ??);
            Grid.SetColumn(txt2, 1);

            txt2.Margin = new Thickness(10, 10, 0, 0);
            grid1.Children.Add(txt2);
        }
        else
        {              
            grid1.Children.Remove(txt2);
        }
    }

2 个答案:

答案 0 :(得分:1)

如果您只想实现这一目标,那么更优雅的方法是将用户控件添加到您的应用程序中,如下所示:

<UserControl x:Class="WpfApplication4.TestUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBox Name="TextBox1"/>
        <TextBox Grid.Column="1">
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Setter Property="Visibility" Value="Collapsed"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Text, ElementName=TextBox1, UpdateSourceTrigger=PropertyChanged}" Value="tea">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

    </Grid>
</UserControl>

然后在你的btn_addnew_Click()方法中,你只需要将这个usercontrol添加到用户Grid并为其分配行和列。显示/隐藏文本框将由用户控件本身处理。

    var userControl = new MyUserControl();
    userControl .Margin = new Thickness(10, 10, 0, 0);
    Grid.SetRow(userControl , i);

    grid1.Children.Add(userControl );

如果你想拥有text.1的Grid.Row值,你可以直接得到它:

        if (textbox1 != null)
        {
           int row = (int)textbox1.GetValue(Grid.RowProperty);
           Grid.SetRow(txt2, row);
        }

答案 1 :(得分:0)

附加属性可用于存储此类信息,因此在类

中定义附加属性
    public static int GetGridRow(DependencyObject obj)
    {
        return (int)obj.GetValue(GridRowProperty);
    }

    public static void SetGridRow(DependencyObject obj, int value)
    {
        obj.SetValue(GridRowProperty, value);
    }

    // Using a DependencyProperty as the backing store for GridRow.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty GridRowProperty =
        DependencyProperty.RegisterAttached("GridRow", typeof(int), typeof(ViewModel), new PropertyMetadata(0));

然后用它来存储行的值

private void btn_addnew_Click(object sender, RoutedEventArgs e)
{
    //1st Column TextBox

    txt1 = new TextBox();
    Grid.SetRow(txt1, i);
    SetGridRow(text1, i);
    ...
    grid1.Children.Add(txt1);
    count++;
}

然后在需要时使用它

//How to set row here?

Grid.SetRow(txt2, GetGridRow(txt1));
Grid.SetColumn(txt2, 1);