在一些DataGrid单元格中创建按钮

时间:2016-10-30 06:58:38

标签: c# wpf datagrid

我有一个绑定到某个集合的数据网格

ObservableCollection<SpecRowViewModel> rows;

项目可能

public class SpecRowViewModel
{
    public ISpecificationItem Item { get; set; }
    public virtual string Name { get { return Item.Name; } set { Item.Name = value; } }
    public virtual string Description { get { return Item.Designation; } set { Item.Designation = value; } }
    public SpecRowViewModel() {}
    public SpecRowViewModel(ISpecificationItem item) { Item = item; }
}

public class RefMaterialRow : SpecRowViewModel
{
    public override  string Name { get { return Item.MaterialPanelData.MaterialNameInBase; } set { Item.MaterialPanelData.MaterialNameInBase = value; } }
    public override string Description { get { return Item.MaterialPanelData.MaterialDesignationInBase; } set { Item.MaterialPanelData.MaterialDesignationInBase = value; } }
    public RefMaterialRow(ISpecificationItem mat) : base() { Item = mat; }
}

public class RefStandartRow: SpecRowViewModel
{
    public override string Name { get { return Item.MaterialPanelData.MaterialNameInBase; } set { Item.MaterialPanelData.MaterialNameInBase = value; } }
    public override string Description { get { return Item.MaterialPanelData.MaterialDesignationInBase; } set { Item.MaterialPanelData.MaterialDesignationInBase = value; } }
    public RefStandartRow(ISpecificationItem mat) : base() { Item = mat; }
}

带转换器的DataGrid单元

<DataGridTemplateColumn x:Name="fd"
                    Width="150"
                    Header="Обозначение">
 <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
                <TextBlock Grid.Row="0"
                           Grid.RowSpan="2"
                           Grid.Column="0"
                           HorizontalAlignment="Left"
                           Text="{Binding Path=Description}"
                           TextWrapping="Wrap" />
                <Button Grid.Row="0"
                           Grid.Column="1"
                           Visibility="{Binding Converter={StaticResource ButtonVisibleConverter}}" />
        </Grid>
    </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我需要使按钮不是表格的所有单元格,而只是在某些单元格中。我做了转换器可见性,但它适用于行中的所有单元格。如何仅在某些单元格中显示按钮?

public class SpecItemToButtonVisibleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var intValue = (SpecRowViewModel) value;
        if (intValue.Item == null) return Visibility.Collapsed;
        if (intValue is RefMaterialRow)
        {
        }
        switch (intValue.Item.Type)
        {
            case EDocumentSection.Parts:
                return Visibility.Visible;
            case EDocumentSection.Materials:
                return Visibility.Visible;
            case EDocumentSection.StandartParts:
                return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
       throw new NotImplementedException();
    }
}

在图片中我更清楚地披露了

Buttons in some cells

1 个答案:

答案 0 :(得分:1)

我认为你必须使用条件数据模板。

Conditional DataTemplate

关于这个话题已经提出了另一个问题:)

好读。