使用 wpf datagridcomboboxcolumn 的 IsSynchronizedWithCurrentItem
(请参阅下面的我自己的答案,这是我在让这个问题渗透了几天之后得出的) 我正在尝试在 WPF 中实现以下场景。
我有一个数据网格,它显示数据行以供查看和附加数据输入。这是一个新应用程序,但有遗留数据。
过去,某个特定字段的数据是随机输入的。现在我们想要将该字段的值限制为特定列表。所以我使用 DataGridComboBoxColumn。 FWIW 我也尝试过使用包含 ComboBox 的 DataGridTemplateColumn 。
在运行时,如果现有值不在列表中,我希望它无论如何都显示。我似乎无法让这种情况发生。虽然我尝试了多种解决方案(全部失败),但这里是最合乎逻辑的解决方案作为起点。
下拉列表的值列表在名为“months”的 Windows 资源中定义。
<DataGridComboBoxColumn x:Name="frequencyCombo" MinWidth="100" Header="Frequency"
ItemsSource="{Binding Source={StaticResource months}}"
SelectedValueBinding="{Binding Path=Frequency,UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
发生的情况是,如果某个值不在列表中,则显示为空白。我已在运行时验证 IsSynchronizedWithCurrentItem 元素确实为 False。它只是没有达到我的预期。
也许我只是在这里走错了路。也许我需要将文本框与组合框结合使用。也许我需要编写一些代码,而不仅仅是 XAML。我花了几个小时尝试不同的事情,并且非常感谢解决方案。我有一些使用此类或该控件的建议,但没有解释如何使用它。
非常感谢!
(see below for my own answer that I came up with after letting this percolate for days & days)
I am trying to achieve the following scenario in WPF.
I have a datagrid that is displaying rows of data for viewing and additional data entry. It is a new app but there is legacy data.
One particular field in the past has had data randomly entered into it. Now we want to limit that field's values to a particular list. So I'm using a DataGridComboBoxColumn. FWIW I have alternatively tried this with a DataGridTemplateColumn containing a ComboBox.
At runtime, if the existing value is not on the list, I want it to display anyway. I just cannot seem to get that to happen. While I have attempted a vast array of solutions (all failures) here is the one that is most logical as a starting point.
The list of values for the drop down are defined in a windows resource called "months".
<DataGridComboBoxColumn x:Name="frequencyCombo" MinWidth="100" Header="Frequency"
ItemsSource="{Binding Source={StaticResource months}}"
SelectedValueBinding="{Binding Path=Frequency,UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
What is happening is that if a value is not on the list then the display is blank. I have verified at runtime that the IsSynchronizedWithCurrentItem element is indeed False. It is just not doing what I am expecting.
Perhaps I am just going down the wrong path here. Maybe I need to use a textbox in combination with the combobox. Maybe I need to write some code, not just XAML. I have spent hours trying different things and would be really appreciative of a solution. I have had a few suggestions to use this class or that control but without explanation of how to use it.
Thanks a bunch!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否尝试过在视图模型中使用 ObservableCollection 或 CollectionViewSource ,而不是混合静态资源和视图模型属性作为列表中项目的源?然后,您可以随意插入和删除非标准项目,并在需要时随时选择(或不选择)它们。因此,“正常”列表将具有正常的月份,但是当出现奇怪的月份时,将其添加到列表中并使其被选中。似乎在视图模型中专门控制会更容易。
Rather than mixing the static resource and the view model property as a source for items on the list, have you tried using an ObservableCollection or CollectionViewSource in the view model? Then you could insert and remove the non-standard items at will and make them selected (or not) whenever you want. So the "normal" list would have the normal months, but when an odd one comes along, add that to the list and make it selected. Seems like it would be easier to control exclusively in the view model.
为什么不直接做这样的事情:
Why not just do something like:
试试这个:
Try this:
我终于解决了这个问题。
诀窍是摆脱组合框列并使用具有用于显示的文本框和用于编辑的组合框的模板。然而,我仍然花了几个小时来解决一个新问题......在组合框中进行选择时,它会修改我在网格中也使用了组合框的任何其他行。猜猜是什么解决了这个问题!我之前尝试使用的 IsSynchronizedWithCurrentItem 属性。 :)
没有丑陋的黑客行为。下拉列表底部没有不可用的选项。没有代码来添加这些额外的值然后清理它们。
我不会取消马克建议的“答案”,因为它使我能够将应用程序交到客户手中,但这正是我正在寻找的解决方案。我发现它埋在“
感谢大家的帮助!
I have finally solved this.
The trick is to get rid of the comboboxcolumn and use a template that has a textbox for display and a combobox for editing. However, I still spent hours with a new problem...when making a selection in the combobox, it would modify any other rows where I had also used the combobox in the grid. Guess what solved the problem! The IsSynchronizedWithCurrentItem property that I was trying to use before. :)
No ugly hacks. No non-usable choices hanging around at the bottom of the dropdown. No code to add those extra values and then clean them up.
I am not going to take away the "Answer" on Mark's suggestion since it enabled me to get the app into my client's hands, but this is the solution I was looking for. I found it buried in a "connect" item after hours of web searching.
Thanks for everyones help!
您能澄清一下这里发生了什么吗?目前尚不清楚运行时的“现有值”是什么 - 如果这是随机输入数据的字段,通过限制它是否意味着您正在运行某种验证逻辑,尽管您仍然希望它显示?
另外,我更倾向于 Silverlight 方面...WPF 也默认为单向绑定吗?
Could you please clarify what's happening here? It's unclear what the "existing value" is at runtime - if this is the field where data is being randomly entered, by limiting it does this mean you're running some sort of validation logic although you still want it to display?
Also, I'm more on the Silverlight side of things...does WPF also default to one way binding?