【问题标题】:What is the best way to implement buttons inside a gridview?在gridview中实现按钮的最佳方法是什么?
【发布时间】:2015-12-14 01:04:02
【问题描述】:

这是用于 UWP 的 C++/CX 和 XAML。假设我有一堂课:

public ref class foo
{
  private:
   String^ title;
   String^ printstring;
 public:
  foo()
  {
     title = "this is my title";
     printstring = "test";
  }
  property String^ Title
  {
     String^ get()
     {
        return title;
     }
  }

}

我有一个向量Platform::Collections::Vector<foo^>^ testbind。我还有一个返回该向量的属性。在我的 XAML 中,我有一个网格视图,其中 ItemSource 设置为 testbind 的属性,而 ItemTemplate 设置为:

<DataTemplate x:DataType="local:foo">
    <StackPanel Height="100" Width="100">
        <TextBlock Text="{x:Bind Title} TextWrapping="WrapWholeWords"/>
        <Button x:Name=button/>
    </StackPanel>
</DataTemplate>

如何做到这一点,以便当我单击按钮时,我可以在代码后面使用 printstring 做一些事情?在这个假设的场景中,所有的 Titles 和 printstrings 都是相同的,但在我的实际项目中,它们对于向量的每个成员都是不同的。

【问题讨论】:

    标签: xaml gridview data-binding uwp c++-cx


    【解决方案1】:

    我没有使用 c++,但我认为您的问题与 xaml 相关,因此我将尝试回答。

    在使用 DataTemplates 时,总是存在无法使用命名元素的问题(无需通过遍历可视化树来找到它们)。您的解决方案是绑定。

    免责声明:这是浏览器代码

     <DataTemplate x:DataType="local:foo">
            <StackPanel Height="100" Width="100">
                <TextBlock Text="{x:Bind Title} TextWrapping="WrapWholeWords"/>
                <Button x:Name=button Command={x:Bind MyCommand, CommandParameter={x:Bind Title}}/>
            </StackPanel>
        </DataTemplate>
    

    然后拿起参数并执行你的逻辑。 我希望这能解决您的问题。

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 1970-01-01
      • 2010-09-24
      • 2021-08-04
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      相关资源
      最近更新 更多