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

时间:2014-06-05 09:45:18

标签: c# wpf popup

我在网格中逐个动态创建一个Button和一个文本框。我的要求是,如果我点击按钮,弹出窗口就会显示出来。如果我从弹出窗口中选择值,我需要绑定相应行的文本框。例如,如果我点击第5行的按钮,我需要将弹出项目值绑定到第5行文本框。我敲击了绑定值到相应行的文本框。这可能很简单,但我无法做到这一点。这是我的代码。

的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:

    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;
    }

2 个答案:

答案 0 :(得分:0)

使用按钮,文本框和弹出窗口创建用户控件。此用户控件应设置为gridview或listview的单元格模板或项目模板。文本框绑定到弹出列表视图的选定值。因此,当用户打开弹出窗口并选择值时,所选值将在相应的文本框中更新。

<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>

答案 1 :(得分:0)

如果要使用Binding,则需要创建一个viewmodel,一个类。并将其绑定到Button和TextBox。单击按钮时,获取视图模型,并通过在列表视图中选择来设置它的值。

代码很混乱,但您可以自己调试和重写代码。

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));
        }
    }