问题描述
我有一个表单,其中包含在 Xaml 中定义的三个“对”{Entry,Button}。每个按钮都旨在对其相应的条目进行操作。 Xaml 中有没有办法将每个条目与其按钮“关联”?我试图避免必须使用三种几乎相同的方法
OnButtonAClicked()
{
act on Entry A
}
OnButtonBClicked()
{
act on Entry B
}
OnButtoncclicked()
{
act on Entry C
}
我想要一种方法
OnButtonClicked()
{
act on my associated Entry
}
有什么好的办法吗?
谢谢。
解决方法
使用绑定(还没有测试过,认为它会起作用)
<Entry x:Name="EntryA" ... .>
<Button BindingContext="{x:Reference Name=EntryA}" Clicked="OnButtonClicked" ... />
<Entry x:Name="EntryB" ... .>
<Button BindingContext="{x:Reference Name=EntryB}" Clicked="OnButtonClicked" ... />
然后
OnButtonCClicked(object sendrer,EventArgs args)
{
Button btn = (Button)sender;
Entry entry = (Entry)btn.BindingContext;
// entry should be a reference to the appropriate entry
}
,
您可以使用 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char word[20];
struct node *next;
}
node;
void print (node n);
node *table[1];
int main(void)
{
// TODO
char word[20];
FILE *file = fopen("list","r");
node *first = malloc(sizeof(node));
table[0] = first;
if (file != NULL)
{
while (fscanf(file,"%s",word) != EOF)
{
node *entry = malloc(sizeof(node));
if (entry != NULL)
{
strcpy (entry->word,word);
entry->next = first->next;
first->next = entry;
}
}
}
print(*first);
}
void print (node n)
{
while(n != NULL)
{
printf("%s\n",n.word);
print(*n.next);
}
}
和 Command
代替 CommandParameter
事件,这样您就可以将相应的 Entry 控件作为参数发送:
Clicked
在您的 ViewModel 或代码隐藏中:
<Entry x:Name="entry1"/>
<Entry x:Name="entry2"/>
<Button Command="{Binding ClickCommand}"
CommandParameter="{Binding Source={x:Reference entry1}}"/>
<Button Command="{Binding ClickCommand}"
CommandParameter="{Binding Source={x:Reference entry2}}"/>