WPF数据网格中的列可以包含不同类型的数据

时间:2016-12-15 00:43:20

标签: c# wpf datagrid

我试图在数据网格中显示不同的行,有时会显示不同的单元格内容。

我对不同的行有不同的类,例如

第1课:

名称 - 说明 - 复选框

第2课:

名称 - 说明 - 文本框(运行时的用户输入) - 复选框

第3类

名称 - 文本框(运行时用户输入)

这些类是通过继承关联的,所以我可以在同一个observablecollection中使用它们。

我想根据我选择添加的类在数据网格中显示这些内容,例如:

ObservableCollection<Rowitem> rowitems = new ObservableCollection<Rowitem>();

rowitems.Add(new Class1("Tom", "Nice", false));

rowitems.Add(new Class2("John", "Strange", Empty textbox , true));

rowitems.Add(new Class3("Roger", Empty Textbox));

..意思是我希望datagrid在第二行的第三列中显示一个空文本框,其中第一行中有一个复选框,第三行中没有任何内容。这可能吗?

2 个答案:

答案 0 :(得分:1)

您可以使用DataTemplates执行此操作:

只需将其添加到Resources的{​​{1}}:

即可
Grid

答案 1 :(得分:0)

这是我的建议:

<Window x:Class="DataGridDynamicCellView.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:DataGridDynamicCellView"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
<Window.DataContext>
    <local:DynamicCellsDataContext />
</Window.DataContext>
<Grid>
    <DataGrid ItemsSource="{Binding DataGridSource}">
        <DataGrid.Resources>
            <DataTemplate DataType="{x:Type local:PresentedByCheckBox}">
                <Grid HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch">
                    <CheckBox HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              IsChecked="{Binding IsChecked,
                                                  UpdateSourceTrigger=PropertyChanged}" />
                </Grid>
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:PresentedByTextBox}">
                <Grid HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch">
                    <TextBlock HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Text="{Binding HelloWorld,
                                              UpdateSourceTrigger=PropertyChanged}" />
                </Grid>
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:PresentedByComplexBox}">
                <StackPanel HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            Orientation="Horizontal">
                    <Ellipse Height="10" Width="10" Fill="Pink"/>
                    <CheckBox HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              IsChecked="{Binding Checked,
                                                  UpdateSourceTrigger=PropertyChanged}" />
                    <TextBlock HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Text="{Binding HelloWorld,
                                              UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
            </DataTemplate>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="BorderBrush" Value="Green" />
                <Setter Property="BorderThickness" Value="2" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <ContentControl Content="{Binding}" />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Resources>
    </DataGrid>
</Grid></Window>

MVVM视图模型:

public class DynamicCellsDataContext:BaseObservableObject
{
    public DynamicCellsDataContext()
    {
        DataGridSource = new ObservableCollection<object>
        {
            new PresentedByTextBox("Hello world!!!"),
            new PresentedByCheckBox(true),
            new PresentedByComplexBox("Hello world!!!", true),
        };
    }
    public ObservableCollection<object> DataGridSource { get; set; }
}

public class PresentedByComplexBox:BaseObservableObject
{
    private string _helloWorld;
    private bool _checked;

    public string HelloWorld    
    {
        get { return _helloWorld; }
        set
        {
            _helloWorld = value;
            OnPropertyChanged();
        }
    }

    public bool Checked
    {
        get { return _checked; }
        set
        {
            _checked = value;
            OnPropertyChanged();
        }
    }

    public PresentedByComplexBox(string helloWorld, bool isChecked)
    {
        HelloWorld = helloWorld;
        Checked = isChecked;
    }
}

public class PresentedByCheckBox:BaseObservableObject
{
    private bool _isChecked;

    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
            OnPropertyChanged();
        }
    }

    public PresentedByCheckBox(bool isChecked)
    {
        IsChecked = isChecked;
    }
}

public class PresentedByTextBox:BaseObservableObject
{
    private string _helloWorld;

    public string HelloWorld
    {
        get { return _helloWorld; }
        set
        {
            _helloWorld = value;
            OnPropertyChanged();
        }
    }

    public PresentedByTextBox(string helloWorld)
    {
        HelloWorld = helloWorld;
    }
}

BaseObservableObject类:

public class BaseObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
    {
        var propName = ((MemberExpression)raiser.Body).Member.Name;
        OnPropertyChanged(propName);
    }

    protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(name);
            return true;
        }
        return false;
    }
}

这就是全部,如果您需要更多示例,请与我联系。

最好的问候。