Debashis Pal
Debashis Pal

Reputation: 21

How to Bind property to Value of Property in xaml

I have a custom class called TextBoxColumn as Follows

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); }
    }
}

When creating DataGrid columns from XAML:

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

In XAML Dictionary, I have defined the Cell Template for this 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>`

How to get the value of FieldName property of TextBoxColumn and Bind it to Text property? How can I achieve it without C# code?

Upvotes: 2

Views: 3027

Answers (2)

opewix
opewix

Reputation: 5083

Give a name to TextBoxColumn control and try to bind it's property by element name

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

Upvotes: 1

brunnerh
brunnerh

Reputation: 185553

You cannot do thing without code, there is no way to bind properties of a binding (in this case you would want Binding.Path to be whatever the FieldName is).

Upvotes: 0

Related Questions