我想定製 DataGridColumnHeader顯示多個文本字段,而不是
僅表示 DataGridColumn.Header酒店所提供的標題文字。
如果我沒有錯過任何東西,我只需創建一個 DataTemplate並綁定到父對象的屬性
。這適用於 DataGridColumn.Header屬性,但
對附加屬性的綁定失敗。綁定DataGrid列DataTemplate中以附加屬性
爲了完整起見,執行附加屬性:
public static class CustomHeader
{
public static string GetUnit(DependencyObject obj) { return (string)obj.GetValue(UnitProperty); }
public static void SetUnit(DependencyObject obj, string value) { obj.SetValue(UnitProperty, value); }
public static readonly DependencyProperty UnitProperty = DependencyProperty.RegisterAttached(
"Unit", typeof(string), typeof(CustomHeader), new FrameworkPropertyMetadata(null));
}
在XAML的標記使用方法:
<DataGrid x:Name="tObjectDataGrid" Margin="10,50,10,10"
AutoGenerateColumns="False" EnableRowVirtualization="True"
ItemsSource="{Binding ObjectList}"
RowDetailsVisibilityMode="VisibleWhenSelected" >
<DataGrid.Resources>
<DataTemplate x:Key="CustomHeaderTemplate">
<StackPanel>
<TextBlock Text="{Binding}" />
<TextBlock Text="{Binding Path=(cust:CustomHeader.Unit)}" /> <-- attached binding doesn't work :(
</StackPanel>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn x:Name="SpeedColumn"
Width="1*"
Binding="{Binding Speed}"
Header="Speed"
HeaderTemplate="{StaticResource CustomHeaderTemplate}"
cust:CustomHeader.Unit="[m/s]" />
</DataGrid.Columns>
</DataGrid>
我真的很感激任何意見或網絡鏈接來闡明我缺少什麼這裏。 在此先感謝。
你在哪裏設置這個'附property'? –
它在DataGridTextColumn的最後一行中設置... cust:CustomHeader.Unit =「[m/s]」。類'CustomHeader'位於與測試項目其餘部分相同的程序集(在一個測試項目中)和相同的名稱空間中。 「cust」是xmlns:cust =「clr-namespace:DependencyTest」(待完成)...這個設置是否可能會引入意想不到的副作用? – LaWi
行爲是完全正確的,因爲您在'DataGridTextColumn'和'HeaderTemplate'上設置了附加屬性,而不是位於列的'Visual Tree'中。因此不會繼承附加屬性。如果您將屬性的默認值設置爲「TestString」,則該值將顯示在標題中。 –