标签: datagrid

以编程方式将列和行添加到WPF Datagrid

我是WPF的新手.我只想知道如何以编程方式将列和行添加到WPF中的DataGrid.我们过去常常在Windows窗体中执行此操作.创建表列和行,并将其绑定到DataGrid.

我相信WPF DataGrid与ASP.net和Windows形式中使用的有点不同(如果我错了,请纠正我).

我有需要在DataGrid中绘制的行数和列数,以便用户可以编辑单元格中的数据.

c# wpf datagrid silverlight-2.0 wpf-controls

68
推荐指数
5
解决办法
23万
查看次数

从WPF DataGrid复制粘贴数据时,OpenClipboard失败

我有一个使用datagrid的WPF应用程序.该应用程序运行正常,直到我安装Visual Studio 2012和Blend + SketchFlow预览.现在,当我尝试使用Ctrl+ C(在任何应用程序中)将数据从网格复制到剪贴板时,我得到以下异常:

System.Runtime.InteropServices.COMException (0x800401D0): OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo)
   at System.Windows.Clipboard.Flush()
   at System.Windows.Clipboard.CriticalSetDataObject(Object data, Boolean copy)
   at System.Windows.Controls.DataGrid.OnExecutedCopy(ExecutedRoutedEventArgs args)
   at System.Windows.Controls.DataGrid.OnExecutedCopy(Object target, ExecutedRoutedEventArgs args)
   at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
   at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
   at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
   at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
   at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e) …
Run Code Online (Sandbox Code Playgroud)

c# wpf clipboard datagrid copy-paste

68
推荐指数
4
解决办法
4万
查看次数

根据值更改DataGrid单元格颜色

我有一个WPF数据网格,我想根据值不同的单元格颜色.我的xaml下面有代码

Style TargetType="DataGridCell"
Run Code Online (Sandbox Code Playgroud)

但是选择所有行而不是选择一个单元格?我错过了什么?

c# wpf xaml datagrid colors

63
推荐指数
2
解决办法
14万
查看次数

绑定WPF中DataGridColumn的可见性

如何DataGrid通过绑定隐藏WPF中的列?

这就是我做的:

<DataGridTextColumn Header="Column header"
                    Binding="{Binding ColumnValue}"
                    Width="100"
                    ElementStyle="{StaticResource DataGridRightAlign}"
                    Visibility="{Binding MyColumnVisibility}" />
Run Code Online (Sandbox Code Playgroud)

这就是我得到的(除了列仍然可见):

System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement.BindingExpression:路径= MyColumnVisibility; 的DataItem = NULL; target元素是'DataGridTextColumn'(HashCode = 1460142); 目标属性是"可见性"(类型"可见性")

不知道什么是治理手段.在我窗口的某个地方是否有一些总统先生决定哪些有效,哪些无效?或者我必须投票?

在网上搜索解决方案时,我发现有十几页具有很好的标题,但完全不相关或不可复制的内容.所以这似乎是关于这个问题的第一个问题.任何的想法?

wpf xaml datagrid visibility

63
推荐指数
2
解决办法
4万
查看次数

如何在WPF Datagrid上启用滚动条?

当我从本文运行以下Northwind WPF Toolkit Datagrid代码时,我得到了一个数据网格,但是没有滚动条,因此用户只能看到部分数据网格.我使用的是2009年3月的最新版本.

我需要指定什么才能使WPF Datagrid具有滚动条?

我尝试将数据网格放在ScrollViewer中,但这没有帮助.

XAML:

<Window x:Class="TestDataGrid566.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="600" Width="800">
    <StackPanel>
        <toolkit:DataGrid x:Name="TheDataGrid" AutoGenerateColumns="True"/>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

后台代码:

using System.Linq;
using System.Windows;
using TestDataGrid566.Model;

namespace TestDataGrid566
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            NorthwindDataContext db = new NorthwindDataContext();
            var customers = from c in db.Customers
                            select c;
            TheDataGrid.ItemsSource = customers;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

wpf datagrid scroll

59
推荐指数
4
解决办法
10万
查看次数

获取DataGrid WPF中的选定行项

我有一个DataGrid,绑定到Database表,我需要获取所选行的内容DataGrid,例如,我想在MessageBox所选行的内容中显示.

示例DataGrid:

在此输入图像描述

所以,如果我选择第二行,我MessageBox必须显示如下内容:646 Jim Biology.

wpf datagrid selecteditem

59
推荐指数
4
解决办法
20万
查看次数

在WPF中的dataGridCells上设置填充

简单的问题:如何在WPF中的dataGridCell上设置填充?(一次一个或所有细胞,我不在乎)

我已尝试通过在DataGrid.CellStyle属性上添加setter DataGridCell.Padding以及以DataGridColumn.CellStyle相同方式使用属性而无效来使用该属性.

我也尝试使用该DataGridColumn.ElementStyle物业,没有更多的运气.

我有点卡在那里,有没有人设法在dataGridCell上应用填充?

注意:我会补充说不,我不能使用透明边框来执行此操作,因为我已经将边框属性用于其他内容.我也不能使用margin属性(看起来工作得足够令人惊讶),因为我使用了background属性,我不希望我的单元格之间有任何"空白"空间.

wpf datagrid styling

58
推荐指数
3
解决办法
4万
查看次数

如何让WPF DataGrid单元格正确对齐而不会使新行上的可选区域变小?

我正在使用WPF4.0 DataGrid.当双击新行中的单元格时,除非我已向该列添加单元格样式,否则一切正常.例如,我有一个数字列,我希望数据右对齐,所以xaml看起来像这样

<DataGridTextColumn Binding="{Binding Path=ImpaId}"
                    CellStyle="{StaticResource CellRightAlign}">
     <DataGridTextColumn.Header>
          <TextBlock  Style="{StaticResource DataGridHeader}">Impa</TextBlock>
     </DataGridTextColumn.Header>
</DataGridTextColumn>
Run Code Online (Sandbox Code Playgroud)

共享资源中的样式只是:

<Style x:Key="CellRightAlign">
    <Setter Property="Control.HorizontalAlignment"
            Value="Right" />
</Style>
Run Code Online (Sandbox Code Playgroud)

新行上的最终可选区域在图像中显示为小蓝色区域.这是用户点击的非常小的目标,这恰好是他们想要在新行上开始的最可能的列.

DataGrid单元格,宽度很小

如果我删除CellStyle,该区域按预期工作,但当然我失去了正确的对齐方式.

具有适当宽度的DataGrid单元格

有谁知道如何实现这两个目标?

我试过的事情

  1. 将绑定上的TargetNullValue设置为具有一定宽度的格式.这适用于现有行,但对新行没有影响.
  2. 在列上设置MinWidth,这不会影响新行可选区域的宽度.

有用的东西:

使用@AngelWPF答案中的信息,我可以使用CellStyle更改为使用ElementStyle,如下所示:

<DataGridTextColumn Binding="{Binding Path=ImpaId}"
                    CellStyle="{StaticResource CellRightAlign}">
Run Code Online (Sandbox Code Playgroud)

成为

<DataGridTextColumn Binding="{Binding Path=ImpaId}"
                    ElementStyle="{StaticResource CellRightAlign}">
Run Code Online (Sandbox Code Playgroud)

wpf xaml datagrid

58
推荐指数
4
解决办法
6万
查看次数

如何将工具提示添加到DataGridTextColumn

我正在使用WPFtoolkit DataGrid,我必须将文本换行DataGridTextColumn或者我必须ToolTip在文本列中添加一个.我在网上搜索过,但我找不到合适的解决方案.期待您的宝贵建议......

wpf datagrid tooltip datagridtextcolumn

57
推荐指数
2
解决办法
4万
查看次数

删除所有DataGrid行和单元格边框

我想隐藏(或删除)我的数据网格中所有行(以及随后的单元格)的所有边框,想一个基本的HTML表.我看了一遍,大多数问题似乎是关于造型而不是隐藏它们.

我已经尝试过像这样设置BorderBrush和BorderThickness:

 <DataGrid.RowStyle>
     <Style TargetType="DataGridRow">
         <Setter Property="BorderBrush" Value="Transparent" />
         <Setter Property="BorderThickness" Value="0" />
     </Style>
  </DataGrid.RowStyle>
Run Code Online (Sandbox Code Playgroud)

为CellStyle尝试了相同,但没有骰子,仍然看到边界.

.net c# wpf xaml datagrid

56
推荐指数
1
解决办法
4万
查看次数