我想在代码中执行等效的以下xaml,但我不知道如何获取该文本块元素:
<local:DayOfTheWeekColumn
...
<local:DayOfTheWeekColumn.Header>
<TextBlock
Text="{Binding ...},
ToolTip="{Binding ...} />
</local:DayOfTheWeekColumn.Header>
</local:DayOfTheWeekColumn>
DayOfTheWeekColumn是DataGridTextColumn的子类。我可以很容易地得到Header并设置它的内容,现在我想在代码中设置ToolTip,想办法就是我在上面的xaml中这样做。
干杯,
Berryl
编辑=========
到目前为止,这是DayOfTheWeekColumn的代码。 xaml中的TextBlock是Header的可视化树的一部分,而不是我想要保留在xaml中的东西。我想在代码中访问它的工具提示,所以我可以在那里设置它。
我在想我应该可以访问的Header列上有一个Children属性来查找TextBlock,但还没有找到它。
public class DayOfTheWeekColumn : DataGridTextColumn
{
public static readonly DependencyProperty DowDateProperty = DependencyProperty.RegisterAttached(
"DowDate", typeof (DateTime), typeof (DayOfTheWeekColumn), new PropertyMetadata(OnDateChanged));
public DateTime DowDate
{
get { return (DateTime)GetValue(DowDateProperty); }
set { SetValue(DowDateProperty, value); }
}
private static void OnDateChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) {
var col = (DataGridTextColumn) target;
var date = (DateTime) e.NewValue;
col.Header = date.ToString(Strings.ShortDayOfWeekFormat);
//col.Header.ToolTip = "If Only It Were so Easy!!" <==============
}
public DayOfTheWeekColumn() {
Width = 60;
CanUserReorder = false;
CanUserResize = false;
CanUserSort = false;
}
}
答案 0 :(得分:1)
然而,你是如此接近我不确定你是如何错过它的。)
private static void OnDateChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) {
var col = (DataGridTextColumn) target;
var date = (DateTime) e.NewValue;
var textblock = new TextBlock();
col.Header = textblock;
textblock.Text = date.ToString(Strings.ShortDayOfWeekFormat);
textblock.ToolTip = "It is that easy. :)";
}
答案 1 :(得分:0)
为TextBlock指定一个带有x:Name属性的名称,您应该可以使用该名称在代码中访问它。
<TextBlock x:Name="textBlock1" />