我有一个 Xceed DataGrid,其中为网格定义了 EditTemplate
。
网格显示绑定(bind)到大约 6 列集合的项目列表,EditTemplate
是一个用于输入数量的 TextBox
控件。我希望将 IsReadOnly
属性绑定(bind)到不同的属性,以便该项目具有序列号,IsReadOnly
将设置为 true,以便用户无法输入值。我想绑定(bind)到同一集合中的 SerialNum
属性,并将其传递给转换器以返回 true/false 值。我已经写好了转换器;但是,我在绑定(bind)到要传递给转换器的属性时遇到问题。
我的DataGridCollectionViewSource
足够简单:
<xcdg:DataGridCollectionViewSource x:Key="transferItems" Source="{Binding TransferItems}" />
TransferItems
在我的 ViewModel 中设置,所有列都得到正确绑定(bind)。
对于我的所有通用显示列,它们都通过以下方式正确显示:
<xcdg:Column Title="Serial No." AllowSort="False" FieldName="SerialNum" />
我的问题是定义 xcgd:CellEditor
模板,我很确定我的问题是围绕 RelativeSource
的。我尝试了许多不同的组合,试图从 ViewModel 获取 TransferItems.SerialNum
属性,但没有一个组合有效。
这是我目前拥有的:
<xcdg:Column Title="Xfer Qty Good" TextWrapping="Wrap" ReadOnly="False" Width="50" AllowGroup="False" AllowSort="False" FieldName="TransferQtyGood">
<xcdg:Column.CellEditor>
<xcdg:CellEditor>
<xcdg:CellEditor.EditTemplate>
<DataTemplate>
<TextBox x:Name="QtyGood" Margin="2,2,2,2" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{xcdg:CellEditorBinding}" IsReadOnly="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type xcdg:DataGridCollectionViewSource}}, Path=DataContext.TransferItems.SerialNum, Converter={StaticResource serialToEnabledConverter}}"
/>
</DataTemplate>
</xcdg:CellEditor.EditTemplate>
</xcdg:CellEditor>
</xcdg:Column.CellEditor>
</xcdg:Column>
这会产生运行时错误:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Xceed.Wpf.DataGrid.DataGridCollectionViewSource', AncestorLevel='1''. BindingExpression:Path=DataContext.TransferItems.SerialNum; DataItem=null; target element is 'TextBox' (Name='QtyGood'); target property is 'IsReadOnly' (type 'Boolean')
我明白错误告诉我什么,但我只是获得正确的 RelativeSource
路径。我已经阅读了一些关于 RelativeSource
枚举的有用帖子,但仍然缺少一些内容。
最佳答案
以防万一有人遇到这个问题,我最终能够以这种方式使绑定(bind)工作。关键是定义正确的RelativeSource路径:
<TextBox x:Name="QtyGood" Margin="2,2,2,2" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{xcdg:CellEditorBinding}" GotFocus="Qty_GotFocus" LostFocus="Qty_LostFocus"
IsReadOnly="{Binding RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataRow}}, Path=DataContext.SerialNum, Converter={StaticResource serialToEnabledConverter}}"
/>
关于wpf - 将 XceedData Column EditTemplate 绑定(bind)到不同的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42862040/