【发布时间】:2020-12-22 10:45:24
【问题描述】:
我正在自定义 DataGridCells,以便在角落有一个带有工具提示的小三角形。只有当工具提示有内容并且工具提示的内容将来自 DataGrid 的 ItemsSource 时,该三角形才应显示。
带有三角形的DataGrid:
现在我创建了一个名为 Hint 的附加属性,它将绑定到 ItemsSource 的属性,该属性将具有工具提示的内容。
public class HintAttachedProperty
{
public static void SetHint(DependencyObject obj, string text)
{
obj.SetValue(HintProperty, text);
}
public static string GetHint(DependencyObject obj)
{
return obj.GetValue(HintProperty)?.ToString() ?? string.Empty;
}
public static readonly DependencyProperty HintProperty =
DependencyProperty.RegisterAttached("Hint", typeof(string), typeof(HintAttachedProperty), new FrameworkPropertyMetadata(null));
}
DataGrid 的 xaml,现在附加的属性提示设置为值“A test”:
<local:CustomDataGrid x:Name="datagrid"
AutoGenerateColumns="False"
ItemsSource="{Binding ItemsCollection}"
ClipboardCopyMode="IncludeHeader"
EnableRowsMove="True"
AlternatingRowBackground="LightBlue"
AlternationCount="2">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="IsChecked" Binding="{Binding IsChecked}"/>
<DataGridTextColumn x:Name="Test"
local:IsHighLightedAttachedProperty.IsHighLighted="True"
local:HintAttachedProperty.Hint="A test"
Header="ExperienceInMonth"
Binding="{Binding ExperienceInMonth}">
</DataGridTextColumn>
<DataGridTemplateColumn Header="References">
...
</DataGridTemplateColumn>
<DataGridTextColumn Header="EmployeeName" Binding="{Binding EmployeeName}"/>
<DataGridTextColumn Header="EmployeeAge" Binding="{Binding EmployeeAge}" />
</DataGrid.Columns>
</local:CustomDataGrid>
这里是 DataGridCell 的样式,它控制三角形的可见性,并且应该将附加属性绑定到工具提示的内容:
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="borderSides" BorderThickness="2,0,2,0" Background="Transparent" BorderBrush="Transparent" SnapsToDevicePixels="True">
<Border x:Name="borderTopBottom" BorderThickness="0,2,0,2" Background="Transparent" BorderBrush="Transparent" SnapsToDevicePixels="True">
<Grid>
<Grid x:Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<Polygon x:Name="pol" ToolTip="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource HintConverter}}" Grid.Column="1" HorizontalAlignment="Right" Points="5,0 10,0, 10,5" Stroke="Black" Fill="Black" >
</Polygon>
</Grid>
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource HintConverter}}" Value="{x:Static sys:String.Empty}">
<Setter TargetName="pol" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource HintConverter}}" Value="{x:Null}">
<Setter TargetName="pol" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在绑定 {Binding RelativeSource={RelativeSource Self}, Converter={StaticResource HintConverter}} 我使用DataGridCell内容的转换器来获取附加属性的值,因为当我在做 {Binding path=(local:HintAttachedProperty.Hint)} 它总是返回 null。
转换器代码:
public class HintConverters : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DataGridCell dgc = value as DataGridCell;
if (dgc != null)
{
DataGridColumn col = dgc.Column;
if (col != null)
{
var hint = HintAttachedProperty.GetHint(col);
return hint;
}
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
调试时,我可以在转换器中看到值是“A test”,因此没有隐藏三角形,但是当我将光标放在工具提示上时为空:
提示值:
工具提示为空:
另一方面,如果我直接绑定到样式中 ItemsSource 的属性,例如 Employee Name,ToolTip="{Binding EmployeeName}" 效果很好:
如何将属性的值和附加属性绑定到属性?这可能吗?也许我应该换个方向?
感谢您的关注
【问题讨论】:
-
你的设置器
标签: c# wpf xaml dependency-properties attached-properties