因此,我有一个数据网格,它具有不同颜色的单元格,具体取决于单元格的值。
我还有一个显示更多信息的工具提示。这一切都很好。
但是,我想更改工具提示以显示更多信息并与单元格颜色相同。因此,我认为为我的工具提示创建自定义样式是明智的。所以,我有以下代码。
<Style TargetType="ToolTip">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border CornerRadius="15,15,15,15"
BorderThickness="3,3,3,3"
Background="#AA000000"
BorderBrush="#99FFFFFF"
RenderTransformOrigin="0.5,0.5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"/>
<TextBlock Grid.Row="1"/>
<TextBlock Grid.Row="2"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我有一个绑定(bind)到我的数据网格的对象,如下所示。我想将这三个属性绑定(bind)到工具提示中的三个文本框。
class MyTask
{
public string Name;
public int Code;
public string Description;
}
在我的 DataGrid 中,我执行以下操作将我的数据绑定(bind)到我的数据网格
ItemsSource="{Binding TaskList}"
然后在 DataGridTextColumn 中绑定(bind)到如下所示的属性
DataGridTextColumn Header="Code" Binding="{Binding Code}"
这对我来说很有意义。但是,我不知道在创建自定义工具提示时如何使用绑定(bind)。我读到我可以使用模板绑定(bind)。我仍然不明白我的工具提示如何绑定(bind)到我上面的 xaml 中的 MyTask 类型的对象?
更新 - 希望能让我的问题更清楚
我想知道如何在我的控件模板(为 3 个文本框)中创建绑定(bind),然后在我的代码的主要部分中我如何绑定(bind)到这些文本框。然后我想知道如何为我的控件模板的背景颜色创建绑定(bind),我相信这与 relativesource 有关?
当我阅读其他示例(更改模板属性)时,我看到如下所示的行。我真的不明白你为什么要这样做?如果您没有正确处理下面的行,您是否无法在 Padding 属性上创建绑定(bind)?
<Border Padding="{Binding Padding}" ...>
最佳答案
您不需要 TemplateBindng,因为它用于根据动态使用实现控件的属性设置生成的模板对象以进行布局。参见 this CodePlex article这是一个很好的例子,说明您何时需要此类功能。
您只需在 ToolTip
中设置 TextBlock
元素的绑定(bind)。在这种情况下,您实际上根本不需要模板,除了因为您在所有列单元格中使用相同的工具提示,它会帮助您解决问题,因为您不需要复制粘贴相同的代码三次。您正在寻找与本文类似的内容,Tooltip in DataGrid in WPF .
专门针对您的情况的解决方案如下:
<DataGrid Name="TestGrid1" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}">
<TextBlock.ToolTip>
<ToolTip />
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Code">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Code}">
<TextBlock.ToolTip>
<ToolTip />
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Description">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}">
<TextBlock.ToolTip>
<ToolTip />
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.Resources>
<Style TargetType="ToolTip">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border CornerRadius="15,15,15,15"
BorderThickness="3,3,3,3"
Background="#AA000000"
BorderBrush="#99FFFFFF"
RenderTransformOrigin="0.5,0.5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Name}"/>
<TextBlock Grid.Row="1" Text="{Binding Code}"/>
<TextBlock Grid.Row="2" Text="{Binding Description}"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
</DataGrid>
您在 CellTemplate
中设置 ToolTip
属性,以便弹出的结果 ToolTip
具有相同的 DataContext
作为 DataGrid
中的事件行。这样,您可以在 ToolTip
ContentTemplate
中像往常一样简单地进行属性绑定(bind),因为它可以访问与 DataGrid
相同的所有属性> 对于行。
关于c# - 控制模板 : how to create bindings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34354084/