如何在xaml中使用按钮点击事件等事件

时间:2017-06-07 10:56:31

标签: c# wpf xaml

<layoutToolkit:Accordion.ContentTemplate>
                    <DataTemplate>
                        <ListView ItemsSource="{Binding Attributes,Mode=TwoWay}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <control:AccTemplate BindModel ="{Binding BindModel, Mode=TwoWay}" Height="50" OnShowDetailEmployee="OnShowDetailEmployeeClicked"/>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </DataTemplate>
                </layoutToolkit:Accordion.ContentTemplate>

我的列表视图和自定义templage到我的xaml文件中。我想为我的模板制作活动并在xaml中使用该活动。我试过这样,但我是xaml解析错误。

 public sealed partial class AccTemplate
{
    public List<AttrOptionModel> Options { get; set; }
    public AttributeModel Attribute { get; set; }

    public event EventHandler OnShowDetailEmployee; OnShowDetailEmployee="OnShowDetailEmployeeClicked"

如何制作活动?

1 个答案:

答案 0 :(得分:1)

您应该在定义了DataTemplate的XAML文件的代码隐藏文件中实现事件处理程序。因此,如果您的XAML名为UserControl1.xaml,则应在UserControl1.xaml.cs中实现事件处理程序。

<强> UserControl1.xaml.cs:

private void AccTemplate_OnShowDetailEmployee(object sender, EventArgs e)
{

}

<强> UserControl1.xaml:

<control:AccTemplate BindModel ="{Binding BindModel, Mode=TwoWay}" Height="50" OnShowDetailEmployee="AccTemplate_OnShowDetailEmployee"/>

事件本身在控件类中实现:

public class AccTemplate : UIElement
{
    public List<AttrOptionModel> Options { get; set; }
    public AttributeModel Attribute { get; set; }

    public event EventHandler OnShowDetailEmployee;
}