如何在WPF中的有界DataGrid中显示自己类的某些属性? [英] How can i display a certain Property of a Own Class in a bounded DataGrid in WPF ?

查看:81
本文介绍了如何在WPF中的有界DataGrid中显示自己类的某些属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨社区,

我已经为DataContext设置了自己的Class-Object.

此对象包含一个名为"MergedGerman"的属性,类型:System.Data.DataTable.

在此DataTable的每个Cell中,没有简单的字符串,但是有一个自己创建的名为"Text"的类对象.

此文本类包含一个标题"属性,类型:System.String

Hi Community,

I''ve set a own Class-Object to my DataContext.

this Object contains a Property called "MergedGerman", Type: System.Data.DataTable.

In each Cell of this DataTable, there is no simple string, but a own created class-object called "Text".

This Text-Class contains a "Caption" property, Type: System.String

public class Text: DependencyObject
{
   public static readonly DependencyProperty CaptionProperty =
   DependencyProperty.Register("Caption", typeof(string), typeof(Text));


   public string Caption
   {
      get { return (string)GetValue(CaptionProperty); }
      set { SetValue(CaptionProperty, value); }
   }

   // other Properties
   // ....
}



我已将dataGrids ItemSource绑定到我的DataContext的"MergedGerman"属性-



I''ve bound a dataGrids ItemSource to the "MergedGerman"-Property of my DataContext

<DataGrid x:Name="Dgrid_Original" Margin="0,5,0,0" Width="198"  AutoGeneratingColumn="Dgrid_Original_AutoGeneratingColumn"  ItemsSource ="{Binding Path=MergedGerman}">
</DataGrid>




我的DataGrid通过使用ToString()-Method显示文本对象.

屏幕截图 [




My DataGrid displays the Text-Object by using the ToString()-Method.

Screenshot[]

But I want to display the Caption-Property of my Text-Object.

How works it ?


I''ve tried the following:

1.)


<DataGrid x:Name="Dgrid_Original" Margin="0,5,0,0" Width="198"  AutoGeneratingColumn="Dgrid_Original_AutoGeneratingColumn"  ItemsSource ="{Binding Path=MergedGerman}">

<DataGrid.Columns>
   <DataGridTextColumn Header="Test" Binding="{Binding Caption}"/>
</DataGrid.Columns>
</DataGrid>



2.)



2.)

<DataGrid x:Name=quot;Dgrid_Original" Margin=&

quot;0,5,0,0" Width="198"  AutoGeneratingColumn="Dgrid_Original_AutoGeneratingColumn"  ItemsSource ="{Binding Path=MergedGerman}">

<DataGrid.Columns>
   <DataGridTemplateColumn Header="Test">
      <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <TextBox Text="{Binding Caption}"/>
      </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>



1.)和2.)的结果是一个空文本框:-(

您有想法吗?



The result of 1.) and of 2.) is a empty textbox :-(

Do you have an idea ?

推荐答案

是的:)

太简单了!

Yeah :)

It''s sooo easy !

<DataGrid.Columns>
   <DataGridTextColumn Header="Test" Binding="{Binding NameOfTheColumnOfTheItemSourceWhichHaveToBindToThisDataGridTextColumn.Caption}"/>
</DataGrid.Columns>


这篇关于如何在WPF中的有界DataGrid中显示自己类的某些属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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