2015-12-14 143 views
0

这是用于UWP的C++/CX和XAML。假设我有一个班级:在GridView中实现按钮的最佳方式是什么?

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中,我有一个GridView,用的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在代码后面?在这种假设情况下,所有标题和印记串都是相同的,但在我的实际项目中,它们对于矢量的每个成员都是不同的。

回答

0

我没有使用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> 

然后拿起参数和执行逻辑。 我希望这可以解决您的问题。

相关问题