【问题标题】:Can columns in WPF datagrid hold different types of dataWPF数据网格中的列可以保存不同类型的数据吗
【发布时间】:2017-04-30 11:49:09
【问题描述】:

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

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

第 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));

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

【问题讨论】:

  • 当然可以,难度取决于你想要固定列还是动态列。

标签: c# wpf datagrid


【解决方案1】:

这是我的建议:

<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;
    }
}

就是这样,如果您需要更多示例,请告诉我。

最好的问候。

【讨论】:

    【解决方案2】:

    您可以使用 DataTemplates 做到这一点:

    只需将它们添加到您的GridResources

    <Grid.Resources>
          <DataTemplate DataType="{x:Type local:Class1}">
                 <-- Template for class1 -->
           </DataTemplate>
           <DataTemplate DataType="{x:Type local:Class2}">
                 <-- Template for class2 -->
           </DataTemplate>
    </Grid.Resources>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2019-01-14
      • 1970-01-01
      • 2010-11-14
      • 2021-12-31
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多