我有一个DataGrid包含几个DataGridTemplateColumns.我的问题是当前选择的行将一些单元格前景变为白色,即使文本变为白色.
DataGridTemplateColumns包含TextBlocks的行为正确并变为白色,而DataGridTemplateColumns包含TextBoxs的行在选择行时不会更改.
有谁知道为什么或如何解决这个问题?
我尝试过这个解决方案:但它只能让TextBlocks受到影响,有人知道可能出现什么问题吗?
我需要创建一个通用的DataGridTemplateColumn,以便我可以在我的应用程序中使用它与不同的对象和属性.
这是一些示例代码,我在我的项目中使用
<DataGridTemplateColumn Width="100*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Age}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Age}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)
我需要的代码的通用版本,这样我可以放置DataTemplate在app.xaml和我的代码中引用
以下xaml代码将"Image"与datagrid模板列绑定.
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=Image}" Height="16" Width="16" VerticalAlignment="Top" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
The same thing need to be done in codebehind (c# code)
DataGridTemplateColumn im = new DataGridTemplateColumn();
im.Header = "";
Binding items1 = new Binding();
Run Code Online (Sandbox Code Playgroud)
这就是我试过的......如何将datagridtemplate列绑定到图像?
我有一个DataGrid具有DataGridTemplateColumn和ComboBox它.
<DataGrid GridLinesVisibility="All" AutoGenerateColumns="False" ItemsSource="{Binding TestItemCollection}">
<DataGrid.Columns>
<DataGridTemplateColumn Width="*" Header="Test Column">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Width="150"
HorizontalAlignment="Left"
ItemsSource="{Binding TestChildCollection}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
public ObservableCollection<TestClass> TestItemCollection { get; set; } = new ObservableCollection<TestClass>
{
new TestClass(),
new TestClass(),
new TestClass(),
};
public class TestClass
{
public ObservableCollection<string> TestChildCollection { get; set; } = new ObservableCollection<string>
{
"First test item", "Second test item" , "Third test item" , "Fourth test item"
};
}
Run Code Online (Sandbox Code Playgroud)
当我单击 …
我想将自定义控件添加到数据网格的模板列中.
自定义控件与文本框非常相似,但其中包含一个图标.用户可以单击该图标,并从提示窗口中选择一个项目,然后所选项目将填充到文本框中.
我的问题是当文本框填满时,单击第二列后,文本将消失.如果我用简单的文本框替换自定义控件,结果是相同的.
以下是示例代码:
//Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleGridTest
{
public class Employee
{
public string Department { get; set; }
public int ID { get; set; }
public string Name { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
Mainwindow.xaml
<Window x:Class="SimpleGridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid x:Name="grid" Grid.Row="1" Margin="5" AutoGenerateColumns="False"
RowHeight="25" RowHeaderWidth="10"
ItemsSource="{Binding}"
CanUserAddRows="True" CanUserSortColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Department" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Department}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"
Width="100"/> …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Silverlight应用程序,它广泛使用Prism,MVVM模式和MEF.出于几个原因,我选择遵循View-first方法.
在其中一个视图中有一个DataGrid,该网格的一个列是DataGridTemplateColumn,它只有一个Button.
我想在Button上定义一个Command和一个CommandParameter.Command应该是ViewModel的DelegateCommand.CommandParameter应该是直接来自dataGrid的SelectedItems列表.
我已经尝试了几种方法来执行此操作,但Command或CommandParameter都为null.
它遵循我最初编写的代码:
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Width="15" Height="15" Content=">"
Command="{Binding UpdateSearchParametersCommand}"
CommandParameter="{Binding SelectedItems, ElementName=dataGrid}">
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我最好的方法是什么?
提前谢谢,Gianluca.
问题:
有没有办法DataTemplate在XAML中定义一个并在代码中实例化它(而不是检索单例FindResource)并VisualTree在发送到DataTemplate需要的地方之前修改它,例如DataGridTemplateColumn.CellTemplate?
背景:
我通过自己添加列来显示一个二维数组data[][],并且在XAML中定义了一个知道如何呈现数组中每个元素的数组.但是,每个单元格的默认值是行,即.所以,我需要"参数"的由根视觉元素的设置为每列有约束力的地方是列索引.目前,每次都定义为返回同一实例并在其中检索.除了电话给我的树,而不是加载的本身.我正在寻找一种方法来实例化代码,进行所需的修改并设置为.DataGridDataGridTemplateColumnDataTemplateDataContextdata[x]DataTemplateDataContext"[y]"yDataTemplateDataGrid.ResourcesFindResource()LoadContent()UIElementVisualTreeDataTemplateDataTemplateDataGridTemplateColumn.CellTemplate
BitmapImage im = new BitmapImage();
string path1 = @"C:\abc.png";
im.UriSource=new Uri(path1);
DataGridTemplateColumn one = new DataGridTemplateColumn();
this.dataGrid1.Columns.Add(one);
Run Code Online (Sandbox Code Playgroud)
现在我必须在datagridTemplateColumn中添加BitmapImage im.
如何在列中添加图像?
我希望默认情况下列使用
AutoSizeMode = DisplayedCells;
Run Code Online (Sandbox Code Playgroud)
但我希望也可以调整列的大小,但DisplayedCells类型不允许调整大小..
有任何想法吗?
我注意到排序总是不适用于DataGrid模板化列,是否有解决此问题的方法?
wpf ×7
datagrid ×5
c# ×3
binding ×2
mvvm ×2
autoresize ×1
code-behind ×1
combobox ×1
datagridview ×1
datatemplate ×1
silverlight ×1
winforms ×1
wpfdatagrid ×1
xaml ×1