【发布时间】:2014-10-20 23:46:25
【问题描述】:
我在 WPF 中创建了一个自定义 ContentControl 并将以下模板应用于它:
<Style TargetType="{x:Type local:BdlUserControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:BdlUserControl">
<Grid x:Name="ContentGrid">
<Grid.RowDefinitions>
<RowDefinition Height="22"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="White">
<StackPanel HorizontalAlignment="Right">
<Button Content="Close" Width="50" Name="BtClose" Click="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BtClose_Click}"/>
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<ContentPresenter Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
问题是BtClose没有调用自定义控件的代码隐藏中声明的方法BtClose_Click,如下所示:
public void BtClose_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Test");
}
错误很笼统:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
关于为什么会发生这种情况的任何提示?
【问题讨论】:
-
您无法绑定到事件处理程序,您需要创建一个命令并将按钮命令属性绑定到该命令。
标签: c# wpf events wpf-controls custom-controls