【发布时间】:2019-09-21 18:54:33
【问题描述】:
当我在 Xaml 下运行时出现错误:
System.Windows.Markup.XamlParseException: ''Set connectionId throw an 例外。'行号“18”和行位置“14”。'
内部异常 1:InvalidCastException:无法转换 'System.Windows.Style' 对象到 'System.Windows.Controls.TreeView' 输入。
请不要将此问题标记为重复,因为我在 SO 中检查了所有类似的问题,但找不到有效的答案。
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<DataTemplate x:Key="xxx">
<Grid>
<local:ButtonEx ToolTipService.ToolTipClosing="ButtonEx_ToolTipClosing"/>
</Grid>
</DataTemplate>
<Style x:Key="TreeViewItemStyle" TargetType="TreeViewItem" >
<EventSetter Event="MouseDoubleClick" Handler="TreeViewItem_MouseDoubleClick"/>
</Style>
</Window.Resources>
<Grid>
<TreeView x:Name="treeViewBookmarks" ItemContainerStyle="{StaticResource TreeViewItemStyle}"/>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonEx_ToolTipClosing(object sender, ToolTipEventArgs e)
{
}
private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
}
}
class ButtonEx : Button
{
}
如果我删除:ToolTipService.ToolTipClosing="ButtonEx_ToolTipClosing",或者删除:EventSetter Event="MouseDoubleClick" Handler="TreeViewItem_MouseDoubleClick",或者删除x:Name="treeViewBookmarks" - 没有错误。
如果我使用Button 而不是ButtonEx - 没有错误。
【问题讨论】: