我是WPF的新手.我只想知道如何以编程方式将列和行添加到WPF中的DataGrid.我们过去常常在Windows窗体中执行此操作.创建表列和行,并将其绑定到DataGrid.
我相信WPF DataGrid与ASP.net和Windows形式中使用的有点不同(如果我错了,请纠正我).
我有需要在DataGrid中绘制的行数和列数,以便用户可以编辑单元格中的数据.
我有一个使用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) 我有一个WPF数据网格,我想根据值不同的单元格颜色.我的xaml下面有代码
Style TargetType="DataGridCell"
Run Code Online (Sandbox Code Playgroud)
但是选择所有行而不是选择一个单元格?我错过了什么?
如何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); 目标属性是"可见性"(类型"可见性")
不知道什么是治理手段.在我窗口的某个地方是否有一些总统先生决定哪些有效,哪些无效?或者我必须投票?
在网上搜索解决方案时,我发现有十几页具有很好的标题,但完全不相关或不可复制的内容.所以这似乎是关于这个问题的第一个问题.任何的想法?
当我从本文运行以下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) 我有一个DataGrid
,绑定到Database表,我需要获取所选行的内容DataGrid
,例如,我想在MessageBox
所选行的内容中显示.
示例DataGrid
:
所以,如果我选择第二行,我MessageBox
必须显示如下内容:646 Jim Biology.
简单的问题:如何在WPF中的dataGridCell上设置填充?(一次一个或所有细胞,我不在乎)
我已尝试通过在DataGrid.CellStyle
属性上添加setter DataGridCell.Padding
以及以DataGridColumn.CellStyle
相同方式使用属性而无效来使用该属性.
我也尝试使用该DataGridColumn.ElementStyle
物业,没有更多的运气.
我有点卡在那里,有没有人设法在dataGridCell上应用填充?
注意:我会补充说不,我不能使用透明边框来执行此操作,因为我已经将边框属性用于其他内容.我也不能使用margin属性(看起来工作得足够令人惊讶),因为我使用了background属性,我不希望我的单元格之间有任何"空白"空间.
我正在使用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)
新行上的最终可选区域在图像中显示为小蓝色区域.这是用户点击的非常小的目标,这恰好是他们想要在新行上开始的最可能的列.
如果我删除CellStyle,该区域按预期工作,但当然我失去了正确的对齐方式.
有谁知道如何实现这两个目标?
我试过的事情
有用的东西:
使用@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) 我正在使用WPFtoolkit DataGrid
,我必须将文本换行DataGridTextColumn
或者我必须ToolTip
在文本列中添加一个.我在网上搜索过,但我找不到合适的解决方案.期待您的宝贵建议......
我想隐藏(或删除)我的数据网格中所有行(以及随后的单元格)的所有边框,想一个基本的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尝试了相同,但没有骰子,仍然看到边界.
datagrid ×10
wpf ×10
c# ×4
xaml ×4
.net ×1
clipboard ×1
colors ×1
copy-paste ×1
scroll ×1
selecteditem ×1
styling ×1
tooltip ×1
visibility ×1
wpf-controls ×1