【发布时间】:2020-05-11 10:14:03
【问题描述】:
我想创建一个带有按钮的自定义数据网格列。我希望它可重用,所以我不想在模板中定义按钮的文本,而是在列的依赖属性(ButtonText as string)中定义。
这是我目前的代码(不工作)
<DataGridTemplateColumn x:Class="RafColumnButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d" CanUserResize="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding ButtonText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:RafColumnButton}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我认为它与datacontext有关,但我是wpf的新手。
编辑:这是依赖属性的 rafcolumnButton 代码隐藏
Public Property ButtonText As String
Get
Return GetValue(ButtonTextProperty)
End Get
Set(ByVal value As String)
SetValue(ButtonTextProperty, value)
End Set
End Property
Public Shared ReadOnly ButtonTextProperty As DependencyProperty =
DependencyProperty.Register(NameOf(ButtonText),
GetType(String), GetType(RafColumnButton),
New PropertyMetadata(""))
【问题讨论】: