【发布时间】:2011-05-01 19:52:25
【问题描述】:
在 Silverlight 中,我正在为 ObservableCollection 中的每个项目创建一个按钮。我在具有 ObservableCollection 的对象上添加了一个 ICommand 来处理这个问题。在 XAML 中,我如何从其中一个集合项中恢复到这一点?
LayoutRoot.DataContext 设置为以下类的实例:
public class MainViewModel
{
public ICommand TestCommand { get; protected set; }
public ObservableCollection<string> Test { get; protected set; }
public MainViewModel()
{
Test = new ObservableCollection<string>();
Test.Add("Hello");
TestCommand = new DelegateCommand(Test, CanTest);
}
private void Test(object parameter)
{
Test.Add("Test text");
}
private bool CanTest(object parameter)
{
return true;
}
}
并将其与此 XAML 一起使用:
<StackPanel x:Name="LayoutRoot" Background="White">
<ItemsControl ItemsSource="{Binding Test}" />
<Button Command="{Binding TestCommand}">Push Me</Button> <!-- I can access TestCommand when I bind to it here -->
<ItemsControl ItemsSource="{Binding Test}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}"
Command="{Binding Path=TestCommand, Source=?????}" <!-- But how do I get back to the TestCommand from here? -->
CommandParameter="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
【问题讨论】:
标签: c# silverlight xaml data-binding