• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

RX_3

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

wpf datagrid 插入带控件的自定义列的做法

在datagrid节点下加入如下代码

<DataGridTemplateColumn  Header="模版" MinWidth="50" x:Name="test">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox  SelectionChanged="cellComboxTemplate_SelectionChanged" SelectedValue="{Binding DataContext.TemplateNames, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" ItemsSource="{Binding DataContext.TemplateNames, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" IsEditable="True">
                                      
                                    </ComboBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>

                        </DataGridTemplateColumn>

好的,问题来了,最关键的是数据绑定,这里默认读取的数据源是datagrid中绑定的itemsort,所以在这的combox里默认也会去绑定itemsort的数据字段

笔者这里需要有独立的下拉框数据源设置,处理方式是把整个datagrid的数据源进行复合,再次组合成新的数据源。

绑定的数据源指定,需要指定去读取上级datagrid的context的数据。

在binding中加入如下

RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}

意思是去寻找上层 type为 datagrid的数据源。至此结束。

 

posted on 2023-05-23 04:06  RX_3  阅读(287)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3