MVK*_*MVK 5 c# wpf custom-controls
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomCalc">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<TextBlock Text="welcome" Height="50" Width="150" MouseDown=""/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,(在自定义控件库中的程序i锻炼中)我试图更改控件textblock =>按钮.(文本块充当按钮功能)所以我尝试向文本块添加事件它给出了一条错误消息"确保事件失败",并且此文件名为"Generic.xaml",因此我添加了一个类"Generic.xaml.cs",但显示了相同的错误.请提前解释,请解释为什么会发生以及如何解决.
您需要添加一个x:Class属性以支持XAML文件中的事件处理程序.所以你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:CustomCalc"
x:Class="CustomCalc.Generic">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<TextBlock Text="welcome" Height="50" Width="150" MouseDown="TextBlock_MouseDown"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
至于Generic.xaml.cs:
namespace CustomCalc
{
public partial class Generic : ResourceDictionary
{
private void TextBlock_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外不要忘记合并你ResourceDictionary的App.Xaml文件:
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/CustomCalc;component/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8589 次 |
| 最近记录: |