SilverLight动态增加行怎么实现?
第一行是增加行的按钮,第二行是文本框,后面有一个删除行按钮,
点击增加行按钮,第三行会出现文本框,后面有一个删除行按钮,
继续点击则可继续增加文本框,和删除行按钮,
坐等高手实现功能。!!!!! --------------------编程问答-------------------- 来高手帮忙啊!! --------------------编程问答-------------------- 你点击新增时,你new一个TextBox的对象,并将其添加到当前容器中不就行了,要注意的就是控件的ID你得稍微算一下即可。
--------------------编程问答-------------------- 来人啊啊啊啊啊啊。、 --------------------编程问答-------------------- 2楼,还是不是很会啊。就是在Silverlight里面。。。 --------------------编程问答--------------------
int i =0 ;
TextBox tb = new TextBox();
tb.ID = "btn"+i;
i++;
this.Layout.Children.Add(tb);
删除时
this.Layout.Children.Remove(tb);
还是不是很会啊。就是在Silverlight里面。。。 --------------------编程问答--------------------
没有什么ID。
首先你的宿主容器(就是放这个需求界面的容器)应该是StackPanel,并且是垂直布局的。然后每次都添加一个横向布局的StackPanel,里边有一个TextBox和一个Button。
例如:
<UserControl x:Class="Membership.SilverlightControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<StackPanel x:Name="LayoutRoot" Background="White">
<Button Content="Button" HorizontalAlignment="Left" Height="20" Margin="62,0,0,0" Width="115" Click="Button_Click"/>
</StackPanel>
</UserControl>
using System.Windows.Controls;--------------------编程问答-------------------- 帮你写了一个。以下是代码:
namespace Membership
{
public partial class SilverlightControl1 : UserControl
{
public SilverlightControl1()
{
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
var panel = new StackPanel { Orientation = Orientation.Horizontal };
this.LayoutRoot.Children.Add(panel);
panel.Children.Add(new TextBox { Width = 200 });
panel.Children.Add(new TextBlock { Width = 20 });
var btn = new Button { Content = "删除" };
panel.Children.Add(btn);
btn.Click += (s, arg) => { this.LayoutRoot.Children.Remove(panel); };
}
}
}
<navigation:Page x:Class="SL.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="Page1 Page">
<Grid x:Name="LayoutRoot">
<Button Content="增加行" Height="23" HorizontalAlignment="Left" Margin="32,12,0,0" Name="btnAdd" VerticalAlignment="Top" Width="75" Click="btnAdd_Click" />
<StackPanel x:Name="sp_parent">
<StackPanel Orientation="Horizontal" x:Name="sp1">
<TextBox x:Name="tb1" Height="23" HorizontalAlignment="Left" Margin="32,49,0,0" VerticalAlignment="Top" Width="120" />
<Button x:Name="btn1" Content="删除行" Height="23" HorizontalAlignment="Left" Margin="158,49,0,0" VerticalAlignment="Top" Width="75" />
</StackPanel>
</StackPanel>
</Grid>
</navigation:Page>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
namespace SL
{
public partial class Page1 : Page
{
public int i = 2; //StackPanel编号
public int j = 2; //TextBox编号
public int k = 2; //Button编号
public Page1()
{
InitializeComponent();
}
// 当用户导航到此页面时执行。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
//添加行
//1、首先添加外部容器StackPanel
StackPanel sp = new StackPanel();
sp.Name = sp + i.ToString();
sp.Orientation = Orientation.Horizontal; //设置水平方向
sp.Tag = i; //设置Tag属性为编号,便于后面处理删除事件
i++;
this.sp_parent.Children.Add(sp);
//2、添加TextBox
TextBox tb = new TextBox();
tb.Name = tb + j.ToString();
j++;
sp.Children.Add(tb);
//3、添加Button
Button btn = new Button();
btn.Name = btn + k.ToString();
btn.Content = "删除行";
btn.Click += new RoutedEventHandler(btn_Click);
k++;
sp.Children.Add(btn);
}
void btn_Click(object sender, RoutedEventArgs e)
{
//获取所在StackPanel的ID值
StackPanel sp = ((sender as Button).Parent as StackPanel);
sp.Name = sp+sp.Tag.ToString();
//移除StackPanel
this.sp_parent.Children.Remove(sp);
}
}
}
顺道推荐一下我们的技术群号:106887513 --------------------编程问答--------------------
高手们,我先试试啊。 --------------------编程问答--------------------
高手,删除行,好像用不了诶。。。。 --------------------编程问答--------------------
默认的第一个删除行,一点就报错,。。。。 --------------------编程问答-------------------- 第一个不是属于动态增加的,我就没处理了,你自己处理下,很简单的。
<navigation:Page x:Class="SL.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="Page1 Page">
<Grid x:Name="LayoutRoot">
<Button Content="增加行" Height="23" HorizontalAlignment="Left" Margin="32,12,0,0" Name="btnAdd" VerticalAlignment="Top" Width="75" Click="btnAdd_Click" />
<StackPanel x:Name="sp_parent">
<StackPanel Orientation="Horizontal" x:Name="sp1">
<TextBox x:Name="tb1" Height="23" HorizontalAlignment="Left" Margin="32,49,0,0" VerticalAlignment="Top" Width="120" />
<Button x:Name="btn1" Content="删除行" Height="23" HorizontalAlignment="Left" Margin="158,49,0,0" VerticalAlignment="Top" Width="75" Click="btn1_Click" />
</StackPanel>
</StackPanel>
</Grid>
</navigation:Page>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
namespace SL
{
public partial class Page1 : Page
{
public int i = 2; //StackPanel编号
public int j = 2; //TextBox编号
public int k = 2; //Button编号
public Page1()
{
InitializeComponent();
}
// 当用户导航到此页面时执行。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
//添加行
//1、首先添加外部容器StackPanel
StackPanel sp = new StackPanel();
sp.Name = sp + i.ToString();
sp.Orientation = Orientation.Horizontal; //设置水平方向
sp.Tag = i; //设置Tag属性为编号,便于后面处理删除事件
i++;
this.sp_parent.Children.Add(sp);
//2、添加TextBox
TextBox tb = new TextBox();
tb.Name = tb + j.ToString();
j++;
sp.Children.Add(tb);
//3、添加Button
Button btn = new Button();
btn.Name = btn + k.ToString();
btn.Content = "删除行";
btn.Click += new RoutedEventHandler(btn_Click);
k++;
sp.Children.Add(btn);
}
void btn_Click(object sender, RoutedEventArgs e)
{
//获取所在StackPanel的ID值
StackPanel sp = ((sender as Button).Parent as StackPanel);
sp.Name = sp+sp.Tag.ToString();
//移除StackPanel
this.sp_parent.Children.Remove(sp);
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
//移除第一个StackPanel即可,因为看你需求不是动态的,所以没处理,以为这个你可以的。
this.sp_parent.Children.Remove(sp1);
}
}
}
--------------------编程问答--------------------
高手,我基本已经不会了,还有,我在Add的时候,我想在每一个文本框前加一个TextBlock,怎么加啊? --------------------编程问答--------------------
我在Add的时候,我想在每一个文本框前加一个TextBlock,怎么加啊? --------------------编程问答--------------------
在Add的时候,每个文本框前都是有文字的,还有要加一个下拉列表,像这个文字的形式,我使用的是资源文件。应该怎么实现呢?求高手告知。 --------------------编程问答--------------------
如果使用silverlight3,那么自己修改一下成为new Eventhandler.....代码吧。
为什么不将你的项目设置为silverlight4呢?这个版本已经正式发布有年头了,都快过时了。 --------------------编程问答--------------------
我的项目是silverlight4,刚刚说删除用不了,是我点错拉。。。。。不好意思哦。。嘿嘿。
补充:.NET技术 , ASP.NET