当我离开它时,为什么我的DataGridComboBoxColumn清除其价值? [英] Why is my DataGridComboBoxColumn clearing its value when I navigate away from it?
问题描述
- DataGridComboBoxColumn
- DataGridTextColumn。
我已经设置了数据验证,以便如果有一个值,另一个将会有错误,直到它也有一个值。验证是愚蠢的,但它提供了一些简单的条件来进行验证,所以我可以说明这个问题。
当我在文本单元格中输入内容时,按Tab键,然后单击第一个单元格,第一个单元格显示它处于错误状态(这是正确的)。问题是,当我从组合框下拉菜单中选择某些东西并离开该单元格(通过按Tab键或单击另一个单元格)时,我为组合框选择的值将消失。我有绑定集更新我的源,每当属性更改,所以它被设置为我选择的值,一旦我选择它。但是,当我离开单元格时,属性设置为null。如果单元格不处于错误状态,我不会看到此行为。
有人可以帮忙吗?这是我的DataGrid的XAML:
< DataGrid Grid.Row =2
Name =GrdData
ItemsSource ={Binding Path = Dvm.Data}
SelectedItem ={Binding Path = Dvm.SelectedData,Mode = TwoWay}
CanUserAddRows =True
CanUserDeleteRows =False
AutoGenerateColumns =False
Margin =5
SelectionMode =Single
IsEnabled ={Binding Path = IsGridEnabled}>
< DataGrid.Columns>
< DataGridComboBoxColumn Header =列1
SelectedItemBinding ={Binding Path = Col1,Mode = TwoWay,ValidatesOnDataErrors = True,UpdateSourceTrigger = PropertyChanged}
Width =*
DisplayMemberPath =描述>
< DataGridComboBoxColumn.ElementStyle>
< Style TargetType =ComboBox>
< Setter Property =ItemsSourceValue ={Binding Path = DropDownValues,Mode = OneWay}/>
< Setter Property =IsSynchronizedWithCurrentItemValue =False/>
< / Style>
< /DataGridComboBoxColumn.ElementStyle>
< DataGridComboBoxColumn.EditingElementStyle>
< Style TargetType =ComboBox>
< Setter Property =ItemsSourceValue ={Binding Path = DropDownValues,Mode = OneWay}/>
< Setter Property =IsDropDownOpenValue =True/>
< / Style>
< /DataGridComboBoxColumn.EditingElementStyle>
< / DataGridComboBoxColumn>
< DataGridTextColumn Header =列2
Binding ={Binding Path = Col2,Mode = TwoWay,ValidatesOnDataErrors = True}
Width =*/>
< /DataGrid.Columns>
< / DataGrid>
我无法想象我在做错什么我看到这个其他链接似乎描述了我所遇到的同样的问题但是,为他们工作的解决方案对我来说似乎不起作用;我添加了SelectedValueBinding和SelectedValuePath,但行为没有改变。
删除 Mode = TwoWay
从绑定。
问题是由剪贴板和自动化支持中的错误引起的。通过将单元格上的特殊属性设置为 ClipboardContentBinding
,然后读取该值即可。如果绑定是双向的,则有时会将特殊属性的旧值推回到视图模型,并且验证错误似乎触发了此行为。 DataGridBoundColumns
和 DataGridComboBoxColumns
将提供绑定
或 SelectedItemBinding
如果 ClipboardContentBinding
为null,那么如果将这些设置为 TwoWay
绑定。
如果您没有设置 Mode
,那将是 Default
并使用属性的默认值,即 TwoWay
for TextBox.Text
和 ComboBox.SelectedItem
但 OneWay
为特殊的剪贴板属性。
I have a DataGrid with two columns:
- DataGridComboBoxColumn
- DataGridTextColumn.
I have set up data validation so that if one has a value, the other will be in error until it also has a value. The validation is silly, but it provides some simple criteria with which to do validation so I can illustrate this issue.
When I type something into the text cell, press tab, then click back on the first cell, the first cell shows that it is in an error state (which is correct). The problem is that when I select something from the combo box dropdown and navigate away from that cell (either by pressing tab or by clicking in another cell), the value I selected for the combo box disappears. I have the binding set to update my source whenever the property changes, so it gets set to the value I select as soon as I select it. But, when I navigate away from the cell, the property gets set to null. I do not see this behavior if the cell is not in an error state.
Can anyone help please? Here is the XAML for my DataGrid:
<DataGrid Grid.Row="2"
Name="GrdData"
ItemsSource="{Binding Path=Dvm.Data}"
SelectedItem="{Binding Path=Dvm.SelectedData, Mode=TwoWay}"
CanUserAddRows="True"
CanUserDeleteRows="False"
AutoGenerateColumns="False"
Margin="5"
SelectionMode="Single"
IsEnabled="{Binding Path=IsGridEnabled}">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Column 1"
SelectedItemBinding="{Binding Path=Col1, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Width="*"
DisplayMemberPath="Description">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=DropDownValues, Mode=OneWay}" />
<Setter Property="IsSynchronizedWithCurrentItem" Value="False"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=DropDownValues, Mode=OneWay}"/>
<Setter Property="IsDropDownOpen" Value="True" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTextColumn Header="Column 2"
Binding="{Binding Path=Col2, Mode=TwoWay, ValidatesOnDataErrors=True}"
Width="*"/>
</DataGrid.Columns>
</DataGrid>
I can't imagine what I'm doing wrong. I saw this other link that seems to describe the same problem I am having, but the solution that worked for them doesn't seem to work for me; I added the SelectedValueBinding and SelectedValuePath, but the behavior did not change.
Remove Mode=TwoWay
from the bindings.
The problem is caused by a bug in the clipboard and automation support. That works by setting a special property on the cell to ClipboardContentBinding
and then reading the value. If that binding is two-way, it winds up sometimes pushing an old value from the special property back to the view model, and validation errors seem to trigger this behavior. DataGridBoundColumns
and DataGridComboBoxColumns
will supply Binding
or SelectedItemBinding
if ClipboardContentBinding
is null, so you’ll get this bug if you set either of those to a TwoWay
binding.
If you don’t set Mode
, it will be Default
and use the default from the property, which is TwoWay
for TextBox.Text
and ComboBox.SelectedItem
but OneWay
for the special clipboard property.
这篇关于当我离开它时,为什么我的DataGridComboBoxColumn清除其价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!