DataGrid 内 ComboBox 上的 ItemSsource 绑定

2024-02-07

DataGrid 内的组合框未填充列表。 我认为 ItemSource Path 有问题:

查看(DataGrid 的 xaml 代码):

<DataGrid CanUserAddRows="True" ItemsSource="{Binding Path=GridCollection, Mode=TwoWay}" AutoGenerateColumns="False" IsReadOnly="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Column 1">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Column 2" Width="170" Binding="{Binding Col2, Mode=TwoWay}"/>
</DataGrid>

视图模型(我已经为 ItemModel 创建了一个可观察的集合,当我更新文本列的值并将值分配给模型对象时,它工作正常):

public ObservableCollection<ItemModel> GridCollection
{
    get
    {
        return this.gridCollection;
    }
    set
    {
        this.gridCollection = value;
        base.RaisedPropertyChanged("GridCollection");
    }
}

public List<string> ComboBoxList
{
    get
    {
        return this.comboBoxList;
    }
    set
    {
        this.comboBoxList = value;
        base.RaisedPropertyChanged("GridList");
    }
}

public MultiValueViewModel(string data)
{
    this.GridCollection = new ObservableCollection<ItemModel>(); 
    this.GridCollection.Add(new ItemModel("ABC", 0));
    this.ComboBoxList = new List<string>();
    //Add items to list
}

模型(该模型包含一个具有 2 个属性的类):

public class ItemModel
{
    public ItemModel(string col1, double col2)
    {
        this.Col1 = col1;
        this.Col2 = col2;
    }

    public string Col1 { get; set; }

    public double Col2 { get; set; }
}

我尝试过 Path=ComboBoxList 和 DataContext.ComboBoxList - 两者都不起作用。


尝试这个:

<ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList, RelativeSource={RelativeSource AncestorType=DataGrid}}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/>

The DataContext of the ComboBox is the ItemModel默认情况下,所以你应该绑定到DataContext父母的DataGrid。你可以使用{RelativeSource}如上所述。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DataGrid 内 ComboBox 上的 ItemSsource 绑定 的相关文章

随机推荐

  • 如何更改Android中Webview的字体?

    我想将 webview 的默认字体更改为自定义字体 我正在使用 webview 开发 Android 的双语浏览器应用程序 我尝试通过将自定义字体放入资产中来获取自定义字体的实例 但仍然无法将 webview 的默认字体设置为我的字体 这是
  • 获取触发 jquery Blur() 事件的单击对象[重复]

    这个问题在这里已经有答案了 假设我这样做 target blur function e do stuff 有没有办法获取被单击的对象以触发模糊操作 我尝试使用e target 但这似乎返回附加到模糊操作的对象而不是单击的对象 诀窍是等待额外
  • Java 用空格分隔字符串,不使用 [ ] 括号括起来

    如果字符串没有被这种括号 包围 如何用空格分割字符串 所以字符串 book new interesting book buy it 应该分为 book new interesting book buy it or book new inte
  • 为什么 scalatest MockitoSugar 被弃用?

    我是在 Scala 中编写 junit 测试的新手 我正在使用 Mockito 来模拟对象 我也在用scalatest 2 12 3 0 4 ScalaTest 文档 例如here http www scalatest org user g
  • 如何解决 Firefox 和 Chrome 之间的 CSS 填充问题?

    已经在这几个小时了 我一直在使用这个优秀的 UI 工具包 http getuikit com http getuikit com到目前为止 一切都很棒 问题是在 Chrome 和 Firefox 之间设置我的结果样式 我设置了填充并在 Ch
  • 如何避免类图中的循环关系

    Hi I have a question about some circular relationships that I am facing with my database design I read a few more simila
  • 在 VS 2012 中保存/导出单元测试结果

    如何在 VS 2012 中导出单元测试结果 我知道从来没有直接的方法可以做到这一点 尽管应该有 但至少在 2012 年我能够保存测试结果窗口中的结果 并使用 trx2html 将它们转换为可读的内容 在 2012 年 我没有看到测试资源管理
  • Azure Databricks 和表单识别器 - 图像无效或受密码保护

    我正在尝试使用 Databricks 自动化 Azure 表单识别器流程 我会将 pdf 或 jpg 文件放入 blob 中 并在 Databricks 中运行代码 将文件发送到表单识别器 执行数据识别并将结果放入 blob 中的新 csv
  • 如何防止网站检测到 Fiddler

    问题标题可能没有涵盖整个主题 因为我做了很多研究并发现了许多奇怪的事情 因此 首先 我想要实现的是某种代表用户工作的网站客户端 不做任何非法的事情 只是优化用户的一些工作流程 我已经为很多网站这样做过 效果很好 然而 目前的情况存在一个问题
  • 如何选择 HTML5 画布形状?

    我有一个 HTML5 画布 在上面绘制了几个形状 我想要发生的是 当鼠标单击任何形状时 应该选择该形状 至少它可以告诉选择了哪种形状 谢谢 尝试使用现有的画布库 或创建您自己的 该库在选择形状时会发生事件 下面的示例使用动力学 JS 库 h
  • Backbone.js 和 Require.js:不匹配的匿名 Define() 模块:function (_, Backbone) {

    我刚开始使用 require js 并且将以下 app js 文件作为我的主干应用程序的一部分 require config baseUrl js paths jquery libs jquery 2 1 0 underscore libs
  • Keras ConvLSTM2D:输出层上的 ValueError

    我正在尝试训练 2D 卷积 LSTM 以根据视频数据进行分类预测 然而 我的输出层似乎遇到了问题 ValueError 检查目标时出错 预期dense 1 有 5 个维度 但得到了形状为 1 1939 9 的数组 我当前的模型基于ConvL
  • 在 C# 中接受浮点不准确有什么好处

    我一直在想这个问题last https stackoverflow com questions 4598416 fourier transform rounding error few https stackoverflow com que
  • 当我有 hive-site.xml 时,hive shell 无法打开

    我已经在我的VM播放器中安装了ubuntu 14和hadoop 2 6 0以及Hive 0 14 0 在我的 Hive conf 中没有 Hive site xml 所以我创建了一个新文件 但是当我打开 Hive shell 时出现错误 如
  • knitr kable (Sweave) 中的 LaTeX 数学表达式

    是否可能以及如何在 knit Sweave 报告中使用 LaTeX 数学表达式kable 在下面的例子中 x 2 被呈现为 原样 With xtable 对于下面的示例 我将使用该选项sanitize colnames function f
  • 创建指向非静态类成员函数的类成员指针函数变量

    目标是拥有成员变量 AddValue指向CreateFirstValue类初始化时和第一次调用后的函数AddValue 所有未来对它的调用都将调用CreateAnotherValue 以前 我只有一个AddValue带有条件检查的函数以确定
  • Android 中的 Fling 手势和 Webview

    我有一个 webview 控件 需要支持 Android 中的 fling 手势 以便调出新记录 加载新数据 这是在扩展 Activity 的类中发生的 我见过的所有示例都展示了如何实现对文本视图的手势支持 但没有展示对网络视图的手势支持
  • 使用()创建对象与不使用()创建对象的区别

    我刚刚遇到问题 error request for member show in myWindow which is of non class type MainGUIWindow 当尝试编译一个简单的 qt 应用程序时 include
  • 忽略 New Relic 中的持久 SignalR 连接

    我应该打电话到哪里NewRelic Api Agent NewRelic IgnoreApdex or NewRelic Api Agent NewRelic IgnoreTransaction 在我的 SignalR 集线器中以防止长时间
  • DataGrid 内 ComboBox 上的 ItemSsource 绑定

    DataGrid 内的组合框未填充列表 我认为 ItemSource Path 有问题 查看 DataGrid 的 xaml 代码