简体   繁体   English

如何将弹出窗口值绑定到动态创建的文本框?

[英]how to bind popup value to dynamically created textbox?

i am creating a Button and a textbox dynamically one by one in grid. 我正在网格中一个接一个地动态创建一个Button和一个文本框。 My requirement is, if i click the button, the popup will show. 我的要求是,如果单击按钮,将显示弹出窗口。 and if i select the values from popup, i need to bind the corresponding row's textbox. 如果我从弹出窗口中选择值,则需要绑定相应行的文本框。 Fr example, if i click the 5th row's button, i need to bind the popup item value to the 5th rows textbox. 例如,如果我单击第五行的按钮,则需要将弹出项的值绑定到第五行文本框。 i struck on binding values to corresponding row's textbox. 我想将值绑定到相应行的文本框。 this may be simple one but i am unable to done this. 这可能很简单,但我无法做到这一点。 this is my code., 这是我的代码。

Xaml: XAML:

<Button x:Name="btn_addnewrow" Content="Add" HorizontalAlignment="Left" Margin="50,40,0,0" VerticalAlignment="Top" Width="89" Height="31"    Click="btn_addnewrow_Click"/>
     <Popup Name="popup" IsOpen="False" Placement="Mouse" VerticalOffset="15" HorizontalOffset="0" Margin="124,122,107,65">
         <Border BorderBrush="Black" BorderThickness="1" Background="Coral">
             <StackPanel Orientation="Horizontal" Height="143">
                 <ListView Margin="10,10,0,0" Name="ListView1" HorizontalAlignment="Left"
                  VerticalAlignment="Top" Width="194" Height="133" MouseDoubleClick="ListView1_MouseDoubleClick">
                    <ListViewItem Content="Coffie"></ListViewItem>
                    <ListViewItem Content="Tea"></ListViewItem>
                    <ListViewItem Content="Orange Juice"></ListViewItem>
                    <ListViewItem Content="Milk"></ListViewItem>
                    <ListViewItem Content="Iced Tea"></ListViewItem>
                    <ListViewItem Content="Mango Shake"></ListViewItem>
                </ListView>

            </StackPanel>
        </Border>
    </Popup>

cs: CS:

    public int count = 0;
    public Button btn1;
    public Button btn2;
    public TextBox txt1;

    private void btn_addnewrow_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();
        ColumnDefinition col2 = new ColumnDefinition();

        col0.Width = new GridLength(50);
        col1.Width = new GridLength(100);
        col2.Width = new GridLength(70);


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

        int i = count;

        ////1st Column button
        btn1 = new Button();
        btn1.Margin = new Thickness(10, 10, 0, 0);
        btn1.BorderThickness = new Thickness(0);
        Grid.SetRow(btn1, i);
        Grid.SetColumn(btn1, 0);
        btn1.Tag = btn1;
        btn1.Click += btnBindList_Click;
        grid1.Children.Add(btn1);         

        //2nd column Textbox 
        txt1 = new TextBox();
        txt1.Margin = new Thickness(10, 10, 0, 0);
        txt1.Name = "txt" + i;
        Grid.SetRow(txt1, i);
        Grid.SetColumn(txt1, 1);
        txt1.Tag = txt1;
        grid1.Children.Add(txt1);

        count++;
    }

    private void btnBindList_Click(object sender, RoutedEventArgs e)
    {
          ?
          ?
        popup.IsOpen = true;
    }

    private void ListView1_MouseDoubleClick(object sender, RoutedEventArgs e)
    {
        txt1.Text = (ListView1.SelectedItem as ListViewItem).Content.ToString();
        popup.IsOpen = false;
    }

Create a usercontrol with button, textbox and popup. 用按钮,文本框和弹出窗口创建一个用户控件。 This usercontrol should set to the cell template or item template of gridview or listview. 此用户控件应设置为gridview或listview的单元格模板或项目模板。 The textbox is bound to selected value of popup listview. 该文本框绑定到弹出列表视图的选定值。 So when user open popup and choose the value, the selected value will update in corresponding textbox. 因此,当用户打开弹出窗口并选择值时,所选值将在相应的文本框中更新。

<Grid HorizontalAlignment="Left">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Text="{Binding ElementName=ListView1, Path=SelectedValue.Content}" Width="200" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="2"/>
    <ToggleButton Content="Select" Grid.Column="1" HorizontalAlignment="Left" Margin="2" x:Name="btn"/>
    <Popup PlacementTarget="{Binding ElementName=btn}" Placement="Bottom"
           StaysOpen="False"
           IsOpen="{Binding ElementName=btn, Path=IsChecked}">
        <ListView Name="ListView1"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  Width="194"
                  Height="133">
            <ListViewItem Content="Coffie"></ListViewItem>
            <ListViewItem Content="Tea"></ListViewItem>
            <ListViewItem Content="Orange Juice"></ListViewItem>
            <ListViewItem Content="Milk"></ListViewItem>
            <ListViewItem Content="Iced Tea"></ListViewItem>
            <ListViewItem Content="Mango Shake"></ListViewItem>
        </ListView>
    </Popup>
</Grid>

If you want to use Binding, you need to creat a viewmodel,a class. 如果要使用Binding,则需要创建一个viewmodel类。 And binding it to Button and TextBox. 并将其绑定到Button和TextBox。 When you click a button,get the viewmodel, and set it's value by selecting in listview. 单击按钮时,获取视图模型,并通过在列表视图中进行选择来设置其值。

The code is very confused, but you can debug and rewrite it by yourself. 该代码非常混乱,但是您可以自己调试和重写它。

public int count = 0;
    public Button btn1;
    public Button btn2;
    public TextBox txt1;

    private void btn_addnewrow_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();
        ColumnDefinition col2 = new ColumnDefinition();

        col0.Width = new GridLength(50);
        col1.Width = new GridLength(100);
        col2.Width = new GridLength(70);


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

        int i = count;
        Test t = new Test();
        ////1st Column button
        btn1 = new Button();
        btn1.Margin = new Thickness(10, 10, 0, 0);
        btn1.BorderThickness = new Thickness(0);
        Grid.SetRow(btn1, i);
        Grid.SetColumn(btn1, 0);
        Binding binding = new Binding();
        binding.Source = t;
        btn1.SetBinding(Button.TagProperty, binding);
        btn1.Click += btnBindList_Click;
        grid1.Children.Add(btn1);

        Binding binding1 = new Binding("Content");
        binding1.Source = t;
        //2nd column Textbox 
        txt1 = new TextBox();
        txt1.Margin = new Thickness(10, 10, 0, 0);
        txt1.Name = "txt" + i;
        txt1.SetBinding(TextBox.TextProperty, binding1);
        Grid.SetRow(txt1, i);
        Grid.SetColumn(txt1, 1);
        txt1.Tag = txt1;
        grid1.Children.Add(txt1);

        count++;
    }

    private void btnBindList_Click(object sender, RoutedEventArgs e)
    {

        popup.IsOpen = true;
        t = ((Button)sender).Tag as Test;
    }
    Test t;
    private void ListView1_MouseDoubleClick(object sender, RoutedEventArgs e)
    {
        t.Content = (ListView1.SelectedItem as ListViewItem).Content.ToString();
        popup.IsOpen = false;
    }
}

class Test : INotifyPropertyChanged
{
    private string content;

    public string Content
    {
        get { return content; }
        set
        {
            content = value;
            OnPropertyChanged("Content");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM