Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向datatemplate添加按钮并调用其click事件_C#_.net_Wpf_Visual Studio 2010_Xaml - Fatal编程技术网

C# 向datatemplate添加按钮并调用其click事件

C# 向datatemplate添加按钮并调用其click事件,c#,.net,wpf,visual-studio-2010,xaml,C#,.net,Wpf,Visual Studio 2010,Xaml,我正在尝试构建一个WPF应用程序(使用C#.net),希望在其中添加ListBox中的按钮 这是我放在资源字典中的数据模板 <DataTemplate x:Key="MYTemplate"> <StackPanel Margin="4"> <DockPanel> <TextBlock Text="ISBN No:" DockPanel.Dock="Left" Margin="5,0,10,0" Foregr

我正在尝试构建一个WPF应用程序(使用C#.net),希望在其中添加ListBox中的按钮

这是我放在资源字典中的数据模板

<DataTemplate x:Key="MYTemplate">
    <StackPanel Margin="4">
        <DockPanel>
            <TextBlock Text="ISBN No:" DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="AliceBlue" />
            <TextBlock Text="            " />
            <TextBlock Text="{Binding ISBN}" Foreground="LimeGreen" FontWeight="Bold" />
        </DockPanel>
        <DockPanel>
            <TextBlock Text="Book Name:" DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="AliceBlue"/>
            <TextBlock Text="       " />
            <TextBlock Text="{Binding BookName}" Foreground="LimeGreen"  FontWeight="Bold" />
        </DockPanel >
        <DockPanel >
            <TextBlock Text="Publisher Name:" DockPanel.Dock="Left" Margin="5,0,10,0" Foreground="AliceBlue" />
            <TextBlock Text=" " />
            <TextBlock Text="{Binding PublisherName}" Foreground="LimeGreen" FontWeight="Bold"  />
        </DockPanel>
        <DockPanel>
            <Button Name="MyButton" Content="Click Me">

            </Button>
        </DockPanel>
    </StackPanel>
</DataTemplate>

如何将单击事件添加到上述模板中的按钮标记? 另外,单击按钮时将调用的方法应放在何处。

Nilesh

您应该使用将按钮绑定到命令。 例如,如果您的数据项定义如下:

public class MyItem : ViewModelBase
{
    public MyItem()
    {
        ClickMeCommand = new RelayCommand(ClickMe);
    }

    private void ClickMe()
    {
        Debug.WriteLine("I was clicked");
    }

    public string ISBN { get; set; }
    public string BookName { get; set; }
    public string PublisherName { get; set; }

    public ICommand ClickMeCommand { get; set; }
}
然后,这将调用ClickMe方法

<DockPanel>
    <Button Content="Click Me" Command="{Binding ClickMeCommand}" />
</DockPanel>

或者,可以将命令放在父视图模型中:

public class MainViewModel : ViewModelBase
{
    public IEnumerable<MyItem> Items { get; private set; }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        Items = new List<MyItem>
                    {
                        new MyItem{ ISBN = "ISBN", BookName = "Book", PublisherName = "Publisher"}
                    };
        ClickMeCommand = new RelayCommand<MyItem>(ClickMe);
    }

    private void ClickMe(MyItem item)
    {
        Debug.WriteLine(string.Format("This book was clicked: {0}", item.BookName));
    }

    public ICommand ClickMeCommand { get; set; }
public类MainViewModel:ViewModelBase
{
公共IEnumerable项{get;private set;}
/// 
///初始化MainViewModel类的新实例。
/// 
公共主视图模型()
{
项目=新列表
{
新MyItem{ISBN=“ISBN”,BookName=“Book”,Publisher name=“Publisher”}
};
ClickMeCommand=新建中继命令(ClickMe);
}
私有void ClickMe(我的项目)
{
Debug.WriteLine(string.Format(“单击此书:{0}”,item.BookName));
}
公用图标命令并单击命令{get;set;}
}

并坚持这一点

<DockPanel>
    <Button Content="Click Me" CommandParameter="{Binding}" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}, Path=DataContext.ClickMeCommand}" />
</DockPanel>

请注意,上面的代码使用的是MVVM light,我假设您有

  <ListBox ItemTemplate="{StaticResource MyTemplate}" ItemsSource="{Binding Items}"/>


可能重复:如果您使用事件而不是命令,那么请参阅我的答案@xmedeko:commands比events有优势,比如我们可以为命令分配shorcut键,我们可以专门评估是否执行命令,等等@Nilesh Barai yep,command是更好的解决方案。我已经添加了如何使用事件作为替代。您要求单击事件。@Phil谢谢Phil,我在另一个文件中使用DataTemplate,中的绑定:不适用于我,您能帮助我吗?