原文:WPF: WrapPanel 容器的数据绑定(动态生成控件、遍历)

问题:

       有一些CheckBox需要作为选项添加到页面上,但是数目不定。而为了方便排版,我选择用WrapPanel面板来作为父容器。那现在的问题就是如何把这些控件添加到这个WrapPanel里了。我想到了两个方法,第一个是先得到控件数目,然后再动态生成并加载到这个WrapPanel里,第二个是设置数据绑定。我想第一个是可行的,但是项目中还涉及到其它问题,所以这里就选择第二个了。问题来了,在WrapPanel中并没有可以用来设置绑定并实现动态生成的东西,那要怎么解决了?

办法:

新建一个ItemsControl控件,并为ItemsSource绑定数据源,然后把ItemsControl.ItemsPanel设置为WrapPanel,最后为ItemsControl.ItemTemplate中的CheckBox.Content绑定数据。

eg:

1、创建数据源类型。

public class business
{
public string txt { get; set; }
}

 

2、设置数据源

 public MainWindow()
{
this.InitializeComponent();
List<business> che = new List<business>()
{
new business() { txt = "选项1"},
new business() { txt = "选项2"},
new business() { txt = "选项3"},
new business() { txt = "选项4"},
new business() { txt = "选项5"},
new business() { txt = "选项6"},
new business() { txt = "选项7"}
};
ItemsControl.ItemsSource = che;
}

3、Xaml中

<ItemsControl  x:Name="itemsControl"  Background="#B28BB2F1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Padding="3">
<WrapPanel>
<CheckBox Content="{Binding txt}"/>
</WrapPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

调试一下就OK了。

下一篇告诉你怎么遍历这个DataTemplate,并判断哪些checkBox被选中了。

WPF: WrapPanel 容器的数据绑定(动态生成控件、遍历)的更多相关文章

  1. 周末充电之WPF(三 ) .后台动态生成控件

    布局 -连连看: 代码: private void Window_Loaded_1(object sender, RoutedEventArgs e) { //动态创建行 ; i < ; i++ ...

  2. Asp.net中使用文本框的值动态生成控件的方法

    这篇文章主要介绍了Asp.net中使用文本框的值动态生成控件的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 看到一个网友,有论坛上问及,动态的生成checkbox控件,在文本框中输入一个“花 ...

  3. Silverlight动态生成控件实例

    刚学习Silverlight,做了一个动态创建控件的实例 实现结果:根据已有的控件类名称,得到控件的实例化对象 实现思路1:就是定义一个模板文件,将类名做为参数,在silverlight中使用Srea ...

  4. Asp.net web form 动态生成控件的注意事项

    Asp.net页面生命周期 页面初始化          Page_Init   加载View State      LoadViewState    回发数据处理      LoadPostData ...

  5. Winforn中使用代码动态生成控件

    场景 有时候需要根据配置文件在窗体中使用代码动态生成控件. 比如读取xml配置文件中的节点数量,然后在窗体中生成指定数量的RadioGroup控件. 实现 新建一个窗体,在窗体的加载完之后的事件中 p ...

  6. (五)ASP.NET中动态生成控件

    今天被问到如何在ASP.NET 页面中动态创建一批控件,并且希望在后续代码中能访问到这些动态创建的控件.我用下面的例子来解释这个问题 ================================= ...

  7. c# winform动态生成控件与获取动态控件输入的值

    差不多有2年没有写winform程序,一直都是写bs.最近项目需要,又开始着手写一个小功能的winform程序,需要动态获取xml文件的节点个数,生成跟节点个数一样的textbox, 最后还要获取操作 ...

  8. Unity UGUI动态生成控件

    一. 首先你得先清楚RectTransform组件的一些程序控制 1. 先得到UGUI控件上面的RectTransform组件 RectTransform rtr = gameObject.GetCo ...

  9. (四)动态生成控件,点击button添加控件

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. Perl读写Excel简单操作

    Perl读写Excel简单操作 使用模块 Spreadsheet::ParseExcel Spreadsheet::WriteExcel 读Excel #!/usr/bin/perl -w use s ...

  2. [CSS] Conditionally Apply Styles Using Feature Queries @supports

    While browsers do a great job of ignoring styles they don’t understand, it can be useful to provide ...

  3. Qt多线程学习-用例子来理解多线程(转),这个是我看过最好的文章,总结很详细(感觉exec()的作用就是保持线程不退出,这样方便随时处理主线程发来的信号,是一种非常别致的思路)good

    01 class MThread :public QThread 02 { 03 public: 04     MThread(); 05     ~MThread(); 06     virtual ...

  4. deep learning实践经验总结

    近期拿caffe来做图片分类.遇到不少问题,同一时候也吸取不少教训和获得不少经验. 先看样例再总结经验. 这是一个2类分类器.分的是条纹衣服和纯色衣服. 先看几张图片. 条纹衣服:   纯色衣服: w ...

  5. Spring之i18n配置与使用

    Spring的i18n配置: <!-- conf:i18n --> <bean id="messageSource" class="org.spring ...

  6. 【u025】贝茜的晨练计划

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运动方式是每天进行N(1 <= ...

  7. [React Router v4] Style a Link that is Active with NavLink

    We often need to be able to apply style to navigation links based on the current route. In React Rou ...

  8. [React Native] Writing Platform-Specific Components for iOS and Android in React Native

    Learn to write components that render differently on iOS and Android, but present the same API. Firs ...

  9. 【t019】window(线段树做法)

    Time Limit: 2 second Memory Limit: 256 MB [问题描述] 给你一个长度为N 的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右 ...

  10. [Django] Start a new django project

    Assume we already have a env created call 'demo-env': cd demo-env . bin/activate pip install django ...