【问题标题】:how to bind listview selected item to textbox dynamically in wpf如何在wpf中动态地将listview选定的项目绑定到文本框
【发布时间】:2014-05-30 11:08:03
【问题描述】:

我的网格中有 2 列。我将在每一行中动态创建按钮和文本框。对于动态按钮单击,我将显示一个弹出窗口。弹出窗口有一些 ListviewItems。这是我的要求: 1.如果我选择任何 Listview 项目,它应该显示在相应的文本框中。

例如,如果它在第 3 行,我想在位于 (Row3, Column1) 的文本框中绑定 ListviewItem

这是我的 xaml:

<Grid>
      <DockPanel HorizontalAlignment="Left" Height="165" Margin="40,100,0,0"      VerticalAlignment="Top" Width="620">
         <ScrollViewer>
            <Grid x:Name="grid1" Height="auto" Width="580"/>
         </ScrollViewer>
     </DockPanel>
     <Button x:Name="btn_addnewrow" Content="Add" HorizontalAlignment="Left" Margin="69,43,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>
  </Grid>

这是我的代码:

public partial class DummyTypeofControl : Window
  {
      public DummyTypeofControl()
      {
          InitializeComponent();
       }

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

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


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

        int i = count;

        //1st Column button
        btn1 = new Button();
        btn1.Margin = new Thickness(10, 10, 0, 0);
        btn1.BorderThickness = new Thickness(0);
        btn1.Name = "btn1_" + i;
        Grid.SetRow(btn1, i);
        Grid.SetColumn(btn1, 0);
        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);
        grid1.Children.Add(txt1);

        count++;
    }


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


        ?
        ?
        ?
        ?

        popup.IsOpen = false;
    }
  }

我想获取行数并将listview itms动态绑定到相应的行文本框..我不知道如何继续..有人可以帮助我吗?

【问题讨论】:

  • 如果您正确使用 WPF,这将很容易。我们通常不会像您那样“动态地”在 WPF 中做事。相反,我们创建适当的数据模型类DataTemplates 并使用ICommands 来实现此类功能。如果您像这样设置您的项目,您的需求可以在delegate ICommand 中实现,就像SelectedItem.TextValue = SelectedListViewItem.Text 一样简单。
  • 你也可以在 XAML 中声明所有项目,属性为 visibility=collapsed,然后在后面的代码中显示你想要的!

标签: wpf xaml listboxitem


【解决方案1】:

不太明白你想要什么,但试试这个..有点不合常规。

private void btn_addnewrow_Click(object sender, RoutedEventArgs e)
{
 .....
//let the button know about the textbox
 btn1.Tag = txt1;
}

然后,每次点击

private void btnBindList_Click(object sender, RoutedEventArgs e)
{
    //get the linked textbox and keep it stored int txt1 
    txt1 = ((Button)sender).Tag as TextBox;
    popup.IsOpen = true;
}

然后,当你点击列表视图时

    private void ListView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        txt1.Text = (ListView1.SelectedItem as ListViewItem).Content.ToString();

        popup.IsOpen = false;
    }

有更好更干净的方法来做到这一点

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多