我在WPF DataGrid中显示工具提示,但我想自定义它的宽度,使其不大于包含数据的单元格的大小。我尝试将路径设置为“宽度”和“实际宽度”,但它忽略了它们。我做错了什么?
<DataGridTextColumn Binding="{Binding Description}" Header="Message" Width="*">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ToolTipService.ShowDuration" Value="60000" />
<Setter Property="ToolTip">
<Setter.Value>
<TextBlock TextWrapping="Wrap"
Width="{Binding RelativeSource={RelativeSource AncestorType=DataGridCell}, Path=ActualWidth}"
Text="{Binding Description}" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
答案 0 :(得分:1)
我发现这有助于解释它无法正常工作的原因,
How can I turn binding errors into runtime exceptions?
所以你加上这个,
public class BindingErrorListener : TraceListener
{
private Action<string> logAction;
public static void Listen(Action<string> logAction)
{
PresentationTraceSources.DataBindingSource.Listeners
.Add(new BindingErrorListener() { logAction = logAction });
}
public override void Write(string message) { }
public override void WriteLine(string message)
{
logAction(message);
}
}
并将其添加到您的代码后面,
BindingErrorListener.Listen(m => MessageBox.Show(m));
InitializeComponent();
所以基本上结果就是你的代码中有一个绑定错误,
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGridCell', AncestorLevel='1''. BindingExpression:Path=ActualWidth; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Width' (type 'Double')
在将鼠标悬停在DataGridTextColumn之前发生绑定错误,因此我想知道在将TextBox添加到可视树之前是否正在创建绑定,因此它没有祖先等。
到目前为止,这告诉你为什么问题存在,但我找不到解决方案。