如果是我,我会编写一个快速转换器来确定 DataGridRow 的数据上下文是否为 NewItemPlaceholder:
public class IsNamedObjectVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.GetType().Name == "NamedObject")
{
return Visibility.Hidden;
}
else
{
return Visibility.Visible;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后我会在 Delete Button Visibility 添加一个 RelativeSource 绑定:
<DataGridTemplateColumn x:Name="ciDelete" Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnDelete" HorizontalAlignment="Center" Margin="0" Click="btnDelete_Click" Tag="{Binding Path=ciID}" VerticalAlignment="Center"
Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=DataContext, Converter={StaticResource IsNamedObjectVisibilityConverter}}">
<Image Source="Resources/16x16/delete.png" Stretch="Fill" />
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
这可能是也可能不是最好的方法,但它应该有效。
我希望这有帮助。
编辑:我希望很明显,为了简洁起见,我跳过了创建所述转换器实例的部分。此示例假设您有一个按类的确切名称的 Converter 实例。可能很明显,但值得一提。:)