简体   繁体   中英

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?

给TextBoxColumn控件命名,并尝试按元素名称绑定它的属性

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

没有代码,您将无法做任何事情,没有办法绑定绑定的属性(在这种情况下,您希望Binding.Path成为FieldName )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM