Datagrid 行的静态数据是否可以纯粹在 XAML 中定义,即没有背后的代码? [英] Can static data for Datagrid rows be defined purely in XAML i.e. no code behind?

查看:12
本文介绍了Datagrid 行的静态数据是否可以纯粹在 XAML 中定义,即没有背后的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要以 Datagrid 格式显示的静态数据.这些值仅用于显示目的,不会更改.是否可以将它添加为 Datagrid 控件的某种子标签,以便我可以避免代码隐藏中的任何内容?

I have static data that I want to display in Datagrid format. The values are for display purposes only and will not change. Can it be added as some kind of subtag of the Datagrid control so I can avoid anything in the codebehind?

它必须是 Datagrid 控件,因为目的是试验和演示某些带有虚拟内容的 Datagrid UI 功能.

It has to be Datagrid control only as the purpose is to experiment with and demo certain Datagrid UI features with dummy blah blah content.

如果不可能使用纯 XAML 内容,那么为数据网格设置虚拟内容的最佳(快速和脏)方法是什么?可以不写类等吗?

If pure XAML content is not possible then what is the best (quick & dirty) method to set up dummy content for a datagrid? Can it be done without writing classes etc?

推荐答案

检查此 MSDN 页面

由于datagrid使用ItemsControl类似于Combobox或ListBox,所以datagrid应该是相同的逻辑.在那个例子中,他们基本上用纯 XAML 创建了一个完整的对象集合.

Since the datagrid uses ItemsControl similar to Combobox or ListBox, datagrid should be the same logic. In that example they basically create a whole collection of objects in pure XAML.

<XmlDataProvider x:Key="Employees" XPath="/Employees/*">
  <x:XData>
    <Employees xmlns="">
      <Employee Name="Terry Adams" Type="FTE" EmployeeNumber="1" />
      <Employee Name="Claire O&apos;Donnell" Type="FTE" EmployeeNumber="12345" />
      <Employee Name="Palle Peterson" Type="FTE" EmployeeNumber="5678" />
      <Employee Name="Amy E. Alberts" Type="CSG" EmployeeNumber="99222" />
      <Employee Name="Stefan Hesse" Type="Vendor" EmployeeNumber="-" />
    </Employees>
  </x:XData>
</XmlDataProvider>

<DataTemplate x:Key="EmployeeItemTemplate">
  <TextBlock Text="{Binding XPath=@Name}" />
</DataTemplate>


...

<ListBox Name="employeeListBox"
         ItemsSource="{Binding Source={StaticResource Employees}}"
         ItemTemplate="{StaticResource EmployeeItemTemplate}"
         SelectedValue="12345"
         SelectedValuePath="@EmployeeNumber"/>

<TextBlock Text="{Binding ElementName=employeeListBox, 
                  Path=SelectedValue}"/>

这篇关于Datagrid 行的静态数据是否可以纯粹在 XAML 中定义,即没有背后的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆