2012-09-06 19 views
2

绑定属性,属性的值我有一个名为TextBoxColumn自定义类如下如何在XAML

public class TextBoxColumn : DataGridTemplateColumn 
{ 
    public static readonly DependencyProperty FieldNameProperty = DependencyProperty.Register("FieldName", typeof(string), typeof(TextBoxColumn), new PropertyMetadata("")); 
    public string FieldName 
    { 
     get { return (string)GetValue(FieldNameProperty); } 
     set { SetValue(FieldNameProperty, value); } 
    } 
} 

当从XAML创建的DataGrid列:

<DataGrid> 
    <DataGrid.Columns> 
     <local:TextBoxControl FieldName="FirstName"/> 
     <local:TextBoxControl FieldName="LastName"/> 
    </DataGrid.Columns> 
</DataGrid> 

在XAML字典,我已经定义此TextBoxColumn的单元格模板:

<DataTemplate x:Key="TextBoxColumn_CellTemplate"> 
    <TextBox Text="{Binding FieldName}"/> <!-- Here is the problem, if I give FirstName instead of FieldName, it works fine --> 
</DataTemplate>` 

如何获取FieldName属性的值属性TextBoxColumn的y并将其绑定到Text属性?如何在没有C#代码的情况下实现它?

+0

为什么不使用'DataGridTextColumn'财产? –

回答

0

没有代码就无法做事,没有办法绑定绑定的属性(在这种情况下,您希望Binding.PathFieldName)。

0

提供一个名称TextBoxColumn控制和尝试绑定它是由元素名称

<TextBox Text="{Binding ElementName=txtBoxCol, Path=FieldName}"></TextBox> 
+0

它显示了该TextBoxControl的FieldName的值,例如FirstName,LastName等,不与FirstName或LastName绑定(FieldName的值) –