我有一个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
,所以我没有知道如何做到这一点。
感谢您的回答。我怎么会从MyClass里面调用Bindings.Update()呢? '绑定'在'MainPage'中,所以我无法访问它... – justanotherxl
好吧,我想出了一个解决方案,我以前的评论。我把'friend ref class MyClass'放在'MainPage'类中,然后在'MyClass'的构造函数中,我放了'MyClass(MainPage x)'。然后我用'MyClass(this)'调用构造函数'然后通过执行'x-> Bindings-> Update()'来访问'MainPage'的成员。然而,'Bindings-> Update()'只会更新gridview之外的控件。 GridView内部的任何内容都没有更新,如果我没有任何绑定的GridView外的控件,应用程序会抛出一个nullptr异常。有任何想法吗? – justanotherxl
@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