当前位置: 首页 > news >正文

WPF 绑定方式举例

WPF 绑定方式举例

一、如果ItemsControl 控件的ItemsSource要绑定到List类型,可以如下:

List<string> Names = new List<string>();
Names.Add("aaa");
Names.Add("bbb");
<ItemsControl ItemsSource="{Binding Student}" VerticalAlignment="Center">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding .}" Width="60" Height="22" Margin="0 0 10 0"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

二、ItemControl的子项怎么绑定与父项同一级的属性,如下:

<ItemsControl ItemsSource="{Binding YourCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- 绑定到与ItemsControl同一级别的属性 -->
            <TextBlock Text="{Binding 
                RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, 
                Path=DataContext.YourProperty}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

三、ItemsControl中的子项的按键如何绑定与父项同一级的事件,如下:

<ItemsControl ItemsSource="{Binding YourCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Command="{Binding DataContext.TextChangedCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
http://www.dtcms.com/a/121959.html

相关文章:

  • LabVIEW 图像处理中常见的边缘检测算法
  • 发票真伪查验接口集成攻略-PHP批量查验发票真伪
  • 贪心算法(17)(java)可被三整除的最大整数和
  • [CISSP] [9] 安全漏洞,威胁和对策
  • 视觉分析AI赋能智慧水务多场景应用
  • S32K144的m_data_2地址不够存,重新在LD文件中配置地址区域
  • doxygen自动生成文档,注释容易错位的补充
  • 雷电模拟器过检测技术全解析
  • 使用docker 安装向量数据库Milvus
  • 黑马 SpringAI+DeepSeek 实战:从对话机器人到企业级知识库的大模型开发全攻略
  • <《AI大模型应知应会100篇》第8篇:大模型的知识获取方式及其局限性
  • package.json配置项积累
  • 防火墙介绍
  • SpringMvc的请求-获得请求参数
  • 启山智软的营销方法有哪些优势?
  • 大数据(7)Kafka核心原理揭秘:从入门到企业级实战应用
  • 机器学习 Day10 逻辑回归
  • 设计模式 Day 5:夯实观察者模式(Boost 实战精讲)
  • excel中的VBA指令示例(二)
  • 【Java】Java 中不同类型的类详解
  • 【PVR】《Palm Vein Recognition and Large-scale Research based on Deep Learning》
  • 今日行情明日机会——20250409
  • P9242 [蓝桥杯 2023 省 B] 接龙数列
  • Anacond虚拟环境里安装GPU版本Pytorch
  • linux下截图工具的选择
  • Linux权限管理:从入门到实践
  • 网页部署到宝塔服务器上,发送请求报错?org.springframework.data.redis.RedisSystemException,让我来看看
  • 算法训练之动态规划(一)
  • 为什么在Altium原理图中画线会自动链接到附近?
  • Zephyr、FreeRTOS、RT-Thread 定时器区别分析