2015-12-14 203 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> 

然後拿起參數和執行邏輯。 我希望這可以解決您的問題。

相關問題