【问题标题】:adding button to datatemplate and calling its click event将按钮添加到数据模板并调用其单击事件
【发布时间】:2012-02-11 08:14:26
【问题描述】:

我正在尝试构建一个 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>

如何在上述模板中的按钮标签中添加点击事件? 还有我应该把点击按钮时调用的方法放在哪里。

【问题讨论】:

  • 如果你使用事件而不是命令,那么看我的回答stackoverflow.com/a/30727398/254109
  • @xmedeko:命令比事件有优势,比如我们可以为命令分配快捷键,我们可以具体评估是否执行它等等。
  • @Nilesh Barai 是的,命令是更好的解决方案。我添加了如何使用事件作为替代方案。你要求点击事件

标签: c# .net wpf visual-studio-2010 xaml


【解决方案1】:

尼勒什,

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

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; }

}

并绑定到那个

<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}"/>

【讨论】:

  • @Phil 感谢 Phil,我在单独的文件中使用 DataTemplate,绑定在: 不适合我,你能帮帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多