我有一个ObservableCollection,它包含多种类型的视图模型,我想为每个GridViewColumn的CellTemplates中的每个类型创建一个DataTemplate.在这个简单的例子中,我可以创建一个基本的ViewModel,但我希望能够从xaml中完成这个.下面的xaml显示了我想要做的事情,其中一个DataTemplates将用于每个CellTemplate.
如果有一个GridViewColumn.Resources我会在那里定义DataTemplates然后在CellTemplate中使用DataTemplate和ContentPresenter,但我显然不能这样做.我想我可能需要一个TemplateSelector,但我不知道从哪里开始.
<ListView ItemsSource={Binding GenericObservableCollection>
<ListView.View>
<GridView>
<GridViewColumn Header="Type">
<GridViewColumn.CellTemplate>
<DataTemplate DataType="{x:Type vm:ActionInputViewModel}">
<TextBlock Text="Input"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ActionOutputViewModel}">
<TextBlock Text="Output"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Value">
<GridViewColumn.CellTemplate>
<DataTemplate DataType="{x:Type vm:ActionInputViewModel}">
<TextBlock Text="{Binding Property1}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ActionOutputViewModel}">
<TextBlock Text="{Binding Property2}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud) 我希望在WPF数据网格中有一个自定义列类型,其中一部分将是用户输入的文本框.不幸的是,它似乎没有继承数据网格本身的外观 - 它不显示交替颜色,当选择或编辑一行时,相关单元格不会以相同方式突出显示,依此类推.
<DataGridTemplateColumn Header="Name" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<TextBox Text="{Binding DisplayName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)
看起来默认文本框的样式会覆盖数据网格的样式; 有没有办法只使用datagrids风格?我当然可以设置文本框的样式以模仿数据网格,但如果我想添加其他控件,我也必须为每个控件执行此操作.如果我沿着那条路走下去,我将如何根据celltemplate中的datagridrow属性更改样式? - 例如IsSelected.
在我工作的一个应用程序中,我发现了这段代码 -
public class MatrixCellTemplate : ColumnDataTemplate<MatrixCellContainer>
{
}
public class ColumnDataTemplate<T> : DataTemplate where T : FrameworkElement
{
public ColumnDataTemplate()
{
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(T));
VisualTree = factory;
}
}
Run Code Online (Sandbox Code Playgroud)
这MatrixCellTemplate用于设置CellTemplate自定义DataGridTemplateColumn(稍后添加到DataGrid.Columns集合),如下所示 -
<DataGridTemplateColumn.CellTemplate>
<Matrix:MatrixCellTemplate />
</DataGridTemplateColumn.CellTemplate>
Run Code Online (Sandbox Code Playgroud)
我不确定使用这个有FrameworkElementFactory什么好处,如果我直接MatrixCellContainer用作细胞模板,我可以面对什么问题-
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Matrix:MatrixCellContainer>
</Matrix:MatrixCellContainer>
</DataTemplate>
<!--<Matrix:MatrixCellTemplate />-->
</DataGridTemplateColumn.CellTemplate>
Run Code Online (Sandbox Code Playgroud) wpf datatemplate wpfdatagrid celltemplate frameworkelementfactory
我想在DataGrid中为我的所有列创建一个特定的模板.通常的方法是我将在每个Column的DataGrid中多次复制整个XAML for DataTemplate.
有没有什么办法可以将CellTemplate全局定义为资源,然后只需将"Binding"的"Path"属性传递给它,以便它从DataContext中显示正确的项目?
这可能吗 ?
我使用ngGrid来显示我的数据.我想将cellFilter和cellTemplate一起用于特定列,但似乎它们不能一起工作.当我单独使用cellFilter或cellTemplate时,它们完美无缺.基本上,我想以这种方式格式化单元格的值(例如500000 - > 500,000; 1000000 - > 1,000,000),我也希望以红色显示负值.我怎么解决这个问题?谢谢!
$scope.gridOptions = {
data: 'data',
columnDefs: [
{
field: 'Settled',
cellFilter: 'number',
cellTemplate: '<div ng-class="{red: row.getProperty(col.field) < 0}"><div class="ngCellText">{{row.getProperty(col.field)}}</div></div>'
}]
}
Run Code Online (Sandbox Code Playgroud) celltemplate cell-formatting angularjs ng-grid angular-ui-grid
我正在使用WPF和.NET 3.0.
我有一个相对简单的DataTemplate定义为GridView的CellTemplate.我希望DataTemplate的VisualTree属性包含FrameworkElementFactory,但当我尝试从GridViewColumnHeader.Click事件访问它时,该属性为null.为什么VisualTree为null?我需要访问它.这是ListView定义:
<ListView ItemsSource="{Binding Stuff}" GridViewColumnHeader.Click="Header_Click">
<ListView.View>
<GridView>
<GridViewColumn Width="28">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Name="statusImage" Width="16" Height="16" Source="../Images/media_play_green_16x16.png"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
这是事件处理程序:
private void Header_Click(object sender, RoutedEventArgs e)
{
GridViewColumnHeader gvch = e.OriginalSource as GridViewColumnHeader;
// error! VisualTree is null!
//gvch.Column.CellTemplate.VisualTree.GetType();
}
Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的数据模型:
public class Model
{
public string DisplayAs {get;set;} // TextBox, CheckBox, ComboBox
public string Value {get;set;}
public string DisplayName {get;set;} // Row1, Row2, ...
}
Run Code Online (Sandbox Code Playgroud)
现在我想在 Datagrid 中显示这些模型,如下所示:

我怎么能做到这一点?请提供一些示例代码。我用不同类型的 DataTemplateSelectors 尝试了一整天,但我无法让它工作
我正在构建一个 DataGrid,我想根据当前项目的基础数据类型在单元格内切换图像。
问:是否可以应用这种类型的模板切换?最好仅使用 xaml?
项目来源是
ObservableCollection<BaseModel>
Run Code Online (Sandbox Code Playgroud)
其中包含类型的项目
IncidentModel : BaseModel
ServiceModel : BaseModel
Run Code Online (Sandbox Code Playgroud)
这就是我目前所处的位置:
<DataGrid
ItemsSource="{Binding TicketCollection,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"
IsReadOnly="True"
AutoGenerateColumns="False"
DockPanel.Dock="Top">
<DataGrid.Resources>
<DataTemplate DataType="{x:Type models:IncidentModel}">
<Image Source="pack://application:,,,/SMLib;component/Files/Images/16x16/Active_16.png" />
</DataTemplate>
<DataTemplate DataType="{x:Type models:ServiceModel}">
<Image Source="pack://application:,,,/SMLib;component/Files/Images/16x16/IncidentMgmt_AllIncidents_16.png" />
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Typ">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Id" Binding="{Binding Id,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" />
<DataGridTextColumn Header="Titel" Binding="{Binding Title,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" />
<DataGridTextColumn Header="Status" Binding="{Binding Status,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" />
<DataGridTextColumn Header="Erstellung" Binding="{Binding CreatedDate,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" />
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
感谢您提供的一切可能的帮助!
我正在尝试在运行时动态设置ListView.GridView的CellTemplate的DataTemplate。问题是当我这样做时,什么也没发生。我检查了CellTemplate,它不是null,但它的VisualTree属性为null。有什么建议么?
GridViewColumn gvc = new GridViewColumn
{
Header = col.Label ?? col.Name,
DisplayMemberBinding = binding
};
DataTemplate cellTemplate = FindDataTemplate(listView, col.CellTemplate);
if (cellTemplate != null)
gvc.CellTemplate = cellTemplate;
gridView.Columns.Add(gvc);
Run Code Online (Sandbox Code Playgroud) 我有一个ListView,它有一个ObservableCollection作为其ItemsSource,它有几列.其中一个是State列,它根据项目的当前状态显示不同的消息.目前,这是作为一个基本字符串实现的,虽然它的工作原理远非美观或用户友好.我希望能够改变输出的类型,以更恰当地适应项目的状态.
我做了一些研究,并且知道我需要使用CellTemplate来影响显示,但是所有不同类型的模板简直让我无法理解我无法找到下一步的地方.
我的代码(不包括很多其他listview绒毛)如下:
<ListView Name="itemsListView" ItemsSource="{Binding Source={StaticResource listingDataView}}" IsSynchronizedWithCurrentItem="True">
...
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Item Information">
...
<GridViewColumn DisplayMemberBinding="{Binding Path=StatusMessage}" Width="283" Header="Status" HeaderContainerStyle="{StaticResource GVHeaderLeftAlignedStyle}" />
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
是的,这些项目具有硬编码的"状态消息",它与其他与代码实际相关的属性一起更新,导致代码中的其他地方出现难看的重复.(是的,我知道这远非美丽,但我也希望改善这一点.)这个属性会被称为ItemState因为我不是那么有创意.
所以,我的问题是:我如何改变这一列,以便为给定的状态提供最合适的显示?文本描述适用于许多州,但有些是相当冗长的,可能会从带有进度条的文本中获益,并且可能还有某种时间.另一个州可以从拥有可点击的超链接中获益.换句话说,我认为我需要至少3种不同的CellTemplates.
我意识到这是一个相当开放的问题,很大程度上受到某人(=我)的设计错误的困扰,他对WPF的经验相当少,但这正是为什么我希望有经验的人可以用一些基本代码直接指导我在我做出比我已经更糟糕的事情之前.:)
celltemplate ×10
wpf ×9
datagrid ×4
c# ×3
datatemplate ×3
xaml ×2
angularjs ×1
listview ×1
ng-grid ×1
refactoring ×1
types ×1
visual-tree ×1
wpfdatagrid ×1