繁体   English   中英

如何在XAML中将属性绑定到属性值

[英]How to Bind property to Value of Property in xaml

我有一个名为TextBoxColumn的自定义类,如下所示

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>`

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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