附件 属性 无效

Attached property not working

我正在使用DevExpress 控件TreeListControl 来显示数据。此控件具有列,如数据网格。我想在单元格的中心显示值。为此,我正在使用 CellTemplate:

<dxg:TreeListColumn HorizontalHeaderContentAlignment="Center" Header="January">
    <dxg:TreeListColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding RowData.Row.January}" TextAlignment="Center"/>
        </DataTemplate>
    </dxg:TreeListColumn.CellTemplate>
</dxg:TreeListColumn>

但是我有很多列,唯一的区别是要显示的值。所以我决定使用一种样式并传递带有附加 属性 的值。风格:

<Style x:Key="TreeListColumnStyle" TargetType="{x:Type dxg:TreeListColumn}">
   <Setter Property="HorizontalHeaderContentAlignment" Value="Center"/>
   <Setter Property="CellTemplate">
       <Setter.Value>
           <DataTemplate>
               <TextBlock Text="{TemplateBinding ap:BenDatagridValueProperty.DataGridValue}" TextAlignment="Center"/>
           </DataTemplate>
       </Setter.Value>
   </Setter> 
</Style>

这是我的附加属性:

public static class BenDatagridValueProperty
{
    public static readonly System.Windows.DependencyProperty DataGridValueProperty = System.Windows.DependencyProperty.RegisterAttached(
    "DataGridValue",
    typeof(string),
    typeof(BenDatagridValueProperty),
    new System.Windows.FrameworkPropertyMetadata("", System.Windows.FrameworkPropertyMetadataOptions.Inherits));

    public static string GetDataGridValue(System.Windows.DependencyObject obj)
    {
        return (string)obj.GetValue(DataGridValueProperty);
    }

    public static void SetDataGridValue(System.Windows.DependencyObject obj, string value)
    {
        obj.SetValue(DataGridValueProperty, value);
    }
}

重新制作的专栏现在看起来像:

  <dxg:TreeListColumn Header="January" Style="{StaticResource TreeListColumnStyle}" ap:BenDatagridValueProperty.DataGridValue="15">

15只是一个测试值。并且它不会将数据网格列中的值设置为 15(它不会调用方法 SetDataGridValue(DependencyObject obj,字符串值)。如果我将在 AttachedProperty 中写入默认值,那么我可以在单元格中看到默认值。 不确定错误在哪里。

尝试修改 TreeListColumnStyle:

<Style x:Key="TreeListColumnStyle" TargetType="{x:Type dxg:TreeListColumn}">
   <Setter Property="HorizontalHeaderContentAlignment" Value="Center"/>
   <Setter Property="CellTemplate">
       <Setter.Value>
           <DataTemplate>
               <TextBlock ap:BenDatagridValueProperty.DataGridValue="{TemplateBinding ap:BenDatagridValueProperty.DataGridValue}" Text="{Binding Path=(ap:BenDatagridValueProperty.DataGridValue), RelativeSource={RelativeSource Self}}" TextAlignment="Center"/>
           </DataTemplate>
       </Setter.Value>
   </Setter> 
</Style>