【发布时间】:2016-05-11 10:50:01
【问题描述】:
我是 WPF 新手。我正在创建一些示例类以快速创建最终项目。我有CustomControl1 作为基类,Generic 作为基础 xaml,wInherit 作为派生 xaml 和类。我的基本窗口的 xaml 在这里:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Palayeshgah">
<!--x:Class="Palayeshgah.CustomControl1">-->
<Window.Resources>
<Style x:Key="btnClose" TargetType="{x:Type Button}" >
<Setter Property="Template">
...
</Setter>
</Style>
</Window.Resources>
...
<Grid>
<Button Style="{StaticResource btnClose}" Name="btnClose" Content="" HorizontalAlignment="Left" Margin="274,0,0,0" VerticalAlignment="Top" Width="18" Height="21"/>
</Grid>
这是我的派生 xaml:
<ns:CustomControl1 x:Class="Palayeshgah.PL.Base.wInherit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ns="clr-namespace:Palayeshgah"
Title="Inheritance Sample" Height="300" Width="300" Background="Wheat" MouseEnter="CustomControl1_MouseEnter"/>
这是我的基类:
public class CustomControl1 : Window
这是我的派生类:
public partial class wInherit : CustomControl1
这些代码完美地展示了基本设计。但我应该为我的按钮覆盖点击事件。如果我在基本 xaml 中将 Click="btnClose_Click" 添加到 btnClose,我应该在 Generic.xaml 中引入一个类。如下所示:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Palayeshgah"
x:Class="Palayeshgah.CustomControl1">
然后我收到一个错误:
Missing partial modifier on declaration of type 'Palayeshgah.CustomControl1'; another partial declaration of this type exists.
我将partial 添加到CustomControl1,然后错误更改如下:
Partial declarations of 'Palayeshgah.CustomControl1' must not specify different base classes
我挂在这里。
我没有忘记btnClose_Click。这是基类中的点击事件:
protected virtual void btnClose_Click(object sender, RoutedEventArgs e)
{
Close();
}
您可能会问我为什么要将基 xaml 与基类分开。答案是我无法用这种方法展示基本设计。
谁能告诉我如何通过任何方法在 WPF 中使用继承来继承窗口和事件?
【问题讨论】:
标签: wpf events inheritance overriding