在按钮点击的WPF中添加动态控制

时间:2014-03-09 10:44:04

标签: c wpf

enter image description here 我有三个文本框名为TxtDocumentTitle1,TxtDocumentTitle2,TxtDocumentTitle3 最后有一个Add More按钮。客户端可以单击“添加更多按钮”,以便生成命名为TxtDocumentTitle4的文本框。如果需要他/她可以添加更多文本框。

查看

的XAML代码示例
<Grid Height="450" Width="700" Background="White">

   <TextBlock Height="23" HorizontalAlignment="Left"   Margin="67,20,0,0" Name="textBlocKname" Text="Document Title1:" VerticalAlignment="Top" Width="110" />
   <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,87,0,0" Name="textBlockAddress" Text="Document Title2:" VerticalAlignment="Top" Width="110" />
   <TextBlock Height="23" HorizontalAlignment="Left" Margin="67,154,0,0" Name="textBlockCompanyName" Text="Document Title3:" VerticalAlignment="Top" Width="110" />
   <TextBox Height="46" Margin="67,37,87,0" Name="txtDocumentTitle1" VerticalAlignment="Top" FontSize="24" />
   <TextBox Height="46" HorizontalAlignment="Left" Margin="67,106,0,0" Name="txtDocumentTitle3" VerticalAlignment="Top" Width="546" FontSize="24" />
   <TextBox Height="46" HorizontalAlignment="Left" Margin="67,171,0,0" Name="txtDocumentTitle2" VerticalAlignment="Top" Width="546" FontSize="24" />                 

   <Button Content="Add More" Height="37" HorizontalAlignment="Right" Margin="0,223,87,0" Name="btnAddmore" VerticalAlignment="Top" Width="102" />

        </Grid>

2 个答案:

答案 0 :(得分:0)

您可以通过Binding轻松完成此操作。如果您的Window没有ViewModel,请打开窗口的xaml.cs并将其设为:

 public Window1()
 {
        InitializeComponent();
        DataContext = this;
 }

public ObservableCollection<TextBoxVm> Items { get { return _items; } }
private ObservableCollection<TextBoxVm> _items = new ObservableCollection<TextBoxVm>();

如果没有,只需将最后两行添加到窗口的viewModel中。

现在您需要定义一个派生自DependencyObject的类,并将其命名为TextBoxVm。在其中创建两个DependencyProperty(使用propdp片段),如下所示:

public string Text
{
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(TextBoxVm), new UIPropertyMetadata("default text",
    (d,e)=>
    {
        var vm = (TextBoxVm)d;
        var val = (string)e.NewValue;
        MyDataService.FindAndUpdateItemInDatabase(vm.Id, val);//you can access database with something like this
    }));

public string TitleText
{
    get { return (string)GetValue(TitleTextProperty); }
    set { SetValue(TitleTextProperty, value); }
}
public static readonly DependencyProperty TitleTextProperty =
    DependencyProperty.Register("TitleText", typeof(string), typeof(TextBoxVm), new UIPropertyMetadata("default title"));

这将是xaml代码:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <TextBlock Text="{Binding TitleText}"/>
                <TextBox Text="{Binding Text}"/>
            </WrapPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

现在唯一剩下的就是编写Button逻辑。只需在单击Button时将TextBoxVm添加到Items。

 Items.Add(new TextBoxVm { 
     TitleText = string.Format("Document Title{0}:", Items.Count+1)
 });

编辑注意:

这种方法是标准MVVM(期望按钮点击事件,应使用Command完成)。因此,如果您想在代码中添加控件(不推荐),请搜索:

以编程方式将控件添加到wpf网格。

答案 1 :(得分:0)

* Bizz的上述答案给出了我的问题的解决方案* 除此之外,在我很少研究之后我发现了关于依赖性对象的问题,我找到了一个关于依赖性对象的问题。对我这样的WPF新人来说是有帮助的:)

什么是DependencyObject ??

依赖关系对象是所有WPF对象的基础对象。像Buttons TextBox等所有的UI元素以及Paragraph,Italic,Span等内容元素都是从Dependency Object派生的。

依赖项对象用于WPF属性系统。默认情况下,我们在DOT Net CLR中拥有的属性系统是非常基本的。但依赖属性提供了许多附加功能/服务来支持数据绑定。

将任何属性创建为依赖项属性后,您将自动获得以下功能。即。更改通知,验证,回调,继承,数据绑定,样式,默认值等。

如果您需要自己为所有需要这些功能的属性实现所有这些功能,那么这将是一个很大的过程,让您感到痛苦。所以,这些都是Dependency Object类的开箱即用。

基本上依赖对象类包含字典。因此,当设置任何值或检索值时,它将更改值或从该词典中读取。所以,它只不过是一个关键的价值对。

详细信息abouT DependencyObject

http://www.codeproject.com/Articles/140620/WPF-Tutorial-Dependency-Property http://www.pinfaq.com/32/what-is-dependency-object-in-wpf-where-should-i-use-it

相关问题