Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight XAML:引用代码隐藏类_Silverlight_Xaml_Silverlight 3.0_Prism - Fatal编程技术网

Silverlight XAML:引用代码隐藏类

Silverlight XAML:引用代码隐藏类,silverlight,xaml,silverlight-3.0,prism,Silverlight,Xaml,Silverlight 3.0,Prism,假设以下视图模型定义: public class MyObject { public string Name { get; set; } } } 如果我的XAML看起来像这样: <UserControl> <ListBox ItemsSource="{Binding MyList}"> <ListBox.ItemTemplate> <TextBlock Text="{Binding Name}" /> &l

假设以下视图模型定义:

public class MyObject {
public string Name { get; set; }
}

}

如果我的XAML看起来像这样:

<UserControl>
<ListBox ItemsSource="{Binding MyList}">
    <ListBox.ItemTemplate>
        <TextBlock Text="{Binding Name}" />
        <Button Content="Execute My Command" cmd:Click.Command="{Binding Path=MyCommand, ?????????}" cmd:Click.CommandParameter="{Binding}" />
    </ListBox.ItemTemplate>
</ListBox>

如何将我的
按钮
绑定到我的代码隐藏类的
ICommand
属性

我正在使用Prism和SL3.0,我需要将列表框中的每个按钮绑定到视图模型上的同一命令

以前我的
UserControl
有一个特定的名称,我可以使用
ElementName
绑定,但是现在我的
UserControl
在同一父视图中被多次使用,所以我不能再使用这种技术,我也不知道如何在XAML中实现这一点

如果这是我唯一的选择,我可以在代码隐藏中手动执行,但如果可能的话,我宁愿在XAML中声明执行。

您需要一个命令才能执行此操作,因为您不再处于UserControl的上下文中。您已经离开了这个环境,如果没有DataContextProxy之类的东西,就没有很好的方法回到这个环境中。我在我的项目中使用过它,效果很好

public class MyView : UserControl {
public IMyViewModel Model { get; }
<UserControl>
<ListBox ItemsSource="{Binding MyList}">
    <ListBox.ItemTemplate>
        <TextBlock Text="{Binding Name}" />
        <Button Content="Execute My Command" cmd:Click.Command="{Binding Path=MyCommand, ?????????}" cmd:Click.CommandParameter="{Binding}" />
    </ListBox.ItemTemplate>
</ListBox>