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

Nil*_*rai 4 .net c# wpf xaml visual-studio-2010

我正在尝试构建一个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>
Run Code Online (Sandbox Code Playgroud)

如何将click事件添加到上述模板中的button标签?另外,我应该在何处放置单击按钮时将调用的方法.

Phi*_*hil 13

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; }
}
Run Code Online (Sandbox Code Playgroud)

然后这将调用ClickMe方法.

<DockPanel>
    <Button Content="Click Me" Command="{Binding ClickMeCommand}" />
</DockPanel>
Run Code Online (Sandbox Code Playgroud)

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

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; }
Run Code Online (Sandbox Code Playgroud)

}

并绑定到那个

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

请注意,上面的代码使用MVVM灯,我假设你有

  <ListBox ItemTemplate="{StaticResource MyTemplate}" ItemsSource="{Binding Items}"/>
Run Code Online (Sandbox Code Playgroud)