如何使用MVVM设置ItemSource为DataTable的DataGrid模板

本文关键字:DataTable DataGrid 模板 ItemSource 何使用 MVVM 设置 | 更新日期: 2023-09-27 18:13:40

My XAML:

<DataGrid Grid.Row="1" ItemsSource="{Binding PersonTable}" CanUserAddRows="False" CanUserDeleteRows="False"/>

我的ViewModel代码:

PersonTable = new DataTable();
PersonTable.Columns.Add("Name");
PersonTable.Columns.Add("Delete");

我想在AccessType列中获得按钮,但是:

DataRow dataRow = PersonTable.NewRow();
dataRow["Name"] = person.Name;
dataRow["Delete"] = new Button();
PersonTable.Rows.Add(dataRow);

创建带有"System.Windows.Controls"的单元格。按钮的值。我想实现相同的行为,就像使用这个xaml:

<DataGridTemplateColumn Header="Delete">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

如何使用模板时使用数据表?如何约束;数据网格中的人到单元格的颜色?

编辑:以删除列为例。我事先不知道列名和列数

EDIT2:因为误解:删除只是一个例子,我需要在运行时在20或更多的列。我需要能够在单元格中放置按钮并绑定到属性

如何使用MVVM设置ItemSource为DataTable的DataGrid模板

一个简洁的方法是

   <DataGrid ItemsSource="{Binding PersonTable}" 
      AutoGenerateColumns="False" 
      CanUserAddRows="False" Grid.Row="1" >
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"
                                Width="*"/>
            <DataGridTemplateColumn Width="Auto" Header="Delete">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete"
                                Command="{Binding SomeCommand,
                            RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                CommandParameter="{Binding}"
                                Margin="5"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>       

您可以将命令绑定到删除按钮并执行操作。

您可以创建一个自定义的DataGridColumnTemplateSelector。模板选择器将根据预定义的约定(例如:DataTable列名的前缀)返回每个datagrid列的模板。

,这样模板将保持动态和自定义

您的DataGrid:

<DataGrid x:Name="MyDataGrid1" HorizontalAlignment="Left" Margin="89,34,0,0" VerticalAlignment="Top" AutoGenerateColumns="False">
            <DataGrid.Columns>
            </DataGrid.Columns>
        </DataGrid>

现在,我们将使用动态加载XAML。为此,我们需要在单独的文本文件中进行标记。将其Build Action更改为Content,并将Copy to OutputDirectory更改为Always.

myDataGridCol.txt:

<DataGridTemplateColumn 
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ToggleButton Content="{Binding ^colname^}" Background="Red">
                            </ToggleButton>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

向DataGrid动态添加列可以这样做:

foreach (DataColumn col in PersonTable.Columns)
            {
                DataGridTemplateColumn dtcol;
                using (StreamReader reader = new StreamReader(("32678595/DataGridTemplateColumn.txt")))
                {
                    String xamlContent = reader.ReadToEnd();
                    xamlContent = xamlContent.Replace("^colname^", col.ColumnName);
                    dtcol = (DataGridTemplateColumn)(System.Windows.Markup.XamlReader.Parse(xamlContent));
                }
                dtcol.Header = col.ColumnName;
                MyDataGrid1.Columns.Add(dtcol);
            }
            MyDataGrid1.ItemsSource = PersonTable.DefaultView;

将Color绑定到单元格可以使用下面的概念来完成:

<DataGridTemplateColumn Width="100" Header="Color">
    <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
           <Label>
             <Label.Background>
               <SolidColorBrush x:Name="CellColor" Color="{Binding PropertyColor}"/>
             </Label.Background>
           </Label>
         </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

现在,你知道如何动态添加列和绑定背景属性。你可以相应地创建你的ViewModel。

您可以使用Color col = (Color)ColorConverter.ConvertFromString(value);将字符串转换为颜色以方便绑定。

如果你使用字符串来存储颜色值,那么普通绑定就可以工作了。

<Button Content="color" Background="{Binding PropertyColor}"/>