我的网格中有2列。我将在每一行中动态创建Button和文本框。对于动态按钮单击,我将显示一个弹出窗口。 Popup窗口有一些ListviewItems。这是我的要求: 1.如果我选择任何Listview项目,它应该显示在相应的文本框中。
例如,如果它在第3行,我想将ListviewItem绑定在位于(Row3,Column1)的文本框中
这是我的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绑定到相应的行文本框动态..我不知道如何继续进一步..任何人都可以帮助我吗?
答案 0 :(得分:0)
不太明白你想要什么,但试一试..有点不同寻常。
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;
}
有更好更清洁的方法来做到这一点