2015-12-22 96 views
0

我有一个GridView,与此项目模板(XAML):更改GridView中项目的属性?

<DataTemplate x:DataType="local:MyClass"> 
     <TextBlock Text="{x:Bind MyString}"/> 
     <Button Command="{x:Bind command}"/> 
</DataTemplate> 

这里的MyClass(C++/CX)

public ref class MyClass sealed 
{ 
public: 
    MyClass(); 
    property Platform::String^ MyString 
    { 
      String^ get() 
      { 
       return mystring; 
      } 
    } 
    property ICommand^ command = ref new DelegateCommand(ref new ExecuteDelegate(this, &implementcommand), nullptr); 
    private: 
     String^ mystring; 
     void implementcommand(Platform::Object^ param); 
} 

DelegateCommand函数从该样本拉不修改:https://code.msdn.microsoft.com/windowsapps/Command-binding-inside-3cef5eea 权现在,如果我点击gridview中的任何按钮,它将调用implementcommand。不过,我想让implementcommand对TextBlock控件做些什么,例如更改文本的内容。从mainpage类中的任何函数中,如果它不在gridview中,它就像textBlock->Text = ref new String("hello");一样简单。但是,由于控件位于gridview中,并且实现命令函数位于MyClass而不是MainPage,所以我没有知道如何做到这一点。

回答

0

在您的DataTemplate中的TextBlock中,您将Text绑定到MyString属性,该属性(与我所看到的绑定进行判断)存在于与命令相同的类中。

你需要做的是

  1. 在你implementcommand改变的MyString
  2. 的价值
  3. 呼叫 Bindings.Update()

也有另一种方式(之前的经典方式X:绑定可用)

  1. 变化将TextBlock至O的结合模式neWay
  2. 在您的课程中实现INotifyPropertyChanged
  3. 在您的实现命令中更改MyString的值并引发INotifyPropertyChanged事件。
+0

感谢您的回答。我怎么会从MyClass里面调用Bindings.Update()呢? '绑定'在'MainPage'中,所以我无法访问它... – justanotherxl

+0

好吧,我想出了一个解决方案,我以前的评论。我把'friend ref class MyClass'放在'MainPage'类中,然后在'MyClass'的构造函数中,我放了'MyClass(MainPage x)'。然后我用'MyClass(this)'调用构造函数'然后通过执行'x-> Bindings-> Update()'来访问'MainPage'的成员。然而,'Bindings-> Update()'只会更新gridview之外的控件。 GridView内部的任何内容都没有更新,如果我没有任何绑定的GridView外的控件,应用程序会抛出一个nullptr异常。有任何想法吗? – justanotherxl

+0

@justanotherxl查看[这篇文章](https://social.msdn.microsoft.com/Forums/en-US/5e0a094f-00b9-4de8-aca0-4a27b333b54d/xbind-only-working-after-two-updates- classic-bind-works-fine?forum = wpdevelop)来查看调用Bindings.Update的更好方法。你的Page对MyClass的依赖比反过来更好。 我想不出为什么Bindings.Update会更新GridView外的其他所有东西。你改变了GridView的DataContext吗? – Corcus

相关问题