我有一个数据网格,它可以从数据表中获取数据,例如在里面,如果有一个带有上下文菜单的文本框,并且此上下文菜单有一个转换器(具有多重绑定(问题是 i 数据表绑定的数据有 1 或 0 作为数据,但在转换器中我得到 UnsetValue。这是 xaml 代码:
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" VirtualizingStackPanel.VirtualizationMode="Recycling" VirtualizingStackPanel.IsVirtualizing="True" Margin="0,1,0,0" x:Name="TrackingDataGrid" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" IsManipulationEnabled="True" CanUserResizeColumns="False" ItemsSource="{Binding TrackingTable}" ColumnHeaderStyle="{DynamicResource dataGridColumnStyle}" Style="{DynamicResource dataGridStyle}" EnableColumnVirtualization="True" IsEnabled="True" BorderThickness="0" CellStyle="{DynamicResource CellStyleBase}" VerticalGridLinesBrush="#FFA0A7AD" RowHeight="30" MinRowHeight="20" GridLinesVisibility="None" SelectionMode="Single" Grid.Row="1" Grid.ColumnSpan="10" Panel.ZIndex="4" MinHeight="30" Loaded="TrackingDataGrid_Loaded" CanUserReorderColumns="False" VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Process Time" IsReadOnly="True" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DeliveryDate}" TextTrimming="CharacterEllipsis" >
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem x:Name="StandbyProcessMenuITem" Header="StandBy Process" Click="StandbyProcessMenuITem_Click" >
<!--<MenuItem.Visibility>
<MultiBinding Converter="{StaticResource RowUndoButtonVisibility}">
<Binding Path="ProductIsStandby" />
<Binding Path="ProductIsDone" />
</MultiBinding>
</MenuItem.Visibility>-->
</MenuItem>
<MenuItem x:Name="UndoProcessMenuITem" Header="Undo Process" Click="UndoProcessMenuITem_Click" >
<MenuItem.Visibility>
<MultiBinding Converter="{StaticResource RowUndoButtonVisibility}">
<Binding Path="{Binding ProductIsStandby}" />
<Binding Path="{Binding ProductIsDone}" />
</MultiBinding>
</MenuItem.Visibility>
</MenuItem>
<MenuItem x:Name="FinishProcessMenuITem" Header="Finish Process" Click="FinishProcessMenuITem_Click">
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
MenuItem
的DataContext
与CellTemplate
的DataContext
不同,但您可以使用RelativeSource
绑定到ContextMenu
的PlacementTarget
:
<MenuItem x:Name="UndoProcessMenuITem" Header="Undo Process" Click="UndoProcessMenuITem_Click" >
<MenuItem.Visibility>
<MultiBinding Converter="{StaticResource RowUndoButtonVisibility}">
<Binding Path="{Binding PlacementTarget.DataContext.ProductIsStandby, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
<Binding Path="{Binding PlacementTarget.DataContext.ProductIsDone, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
</MultiBinding>
</MenuItem.Visibility>
</MenuItem>