Reputation: 1101
I have found how to create ToolTip
for DataGridColumnHeader
in WPF (and make sure that the Header is wrapped).
<Style x:Key="StartingTabToolTipHeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="ToolTip" Value="{x:Static r:Resource.StartingTabToolTip}"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
I can use this style in this way:
<DataGridComboBoxColumn Header="{x:Static r:Resource.StartingTab}" SelectedItemBinding="{Binding StartingTab}"
HeaderStyle="{StaticResource StartingTabToolTipHeaderStyle}">
This solution is not nice because I need to create a separate style for each header because the tooltip is hardwired in the style. I would like a general style which can be used in this way:
<DataGridComboBoxColumn Header="{x:Static r:Resource.StartingTab}" SelectedItemBinding="{Binding StartingTab}"
MyToolTip="{x:Static r:Resource.StartingTabToolTip}">
Any idea how to do it? May be with attached property?
Upvotes: 0
Views: 360
Reputation: 22079
You can set the ToolTipService.ToolTip
attached property on each column and bind your values to it, because it is not used on columns.
<DataGridComboBoxColumn Header="{x:Static r:Resource.StartingTab}"
SelectedItemBinding="{Binding StartingTab}"
HeaderStyle="{StaticResource StartingTabToolTipHeaderStyle}"
ToolTipService.ToolTip="{x:Static r:Resource.StartingTabToolTip}">
Then you can bind this text on the column in your header style using a RelativeSource
binding.
<Style x:Key="StartingTabToolTipHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ToolTip" Value="{Binding Column.(ToolTipService.ToolTip), RelativeSource={RelativeSource Self}}"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Now you only have a single style. If you want to avoid setting the style on each column, make it an implicit style by omitting the x:Key
. Then it will be applied to all matching types in scope.
<Style TargetType="{x:Type DataGridColumnHeader}">
Upvotes: 1
Reputation: 708
You could use DataGridTemplateColumn
<DataGrid AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="yourHeader" HeaderStyle={StaticResource HeaderStyle1}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<!--you can add any control such as ComboBox, Button etc-->
<TextBlock Text="{Binding Message}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<!--Editing cell template is not mandatory, only if you want allow user to edit a particular cell, shown here only for reference-->
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<!--When the user double clicks on this cell, it changes the control to TextBox (from TextBlock - see above) to allow for user input-->
<TextBox Text="{Binding Message}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<!--Second Column-->
<DataGridTemplateColumn Header="secondHeader" HeaderStyle={StaticResource HeaderStyle2}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemSource="{Binding SomeSource}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
This might seem a bit much for each column. But it gives you far more control & better scalability for future complex changes. Its somewhat trivial work put in during the initial dev phase.
Atleast thats my opinion since after the success of the concept, several further inputs/changes & new requirements come in to improve user interactivity with specific rules & logic on how/when the user can interact with UI Elements especially when altering data.
Edit -
Your style can be exactly like you made i.e. targeting DataGridColumnHeader
.
Upvotes: 0