我有一个窗口和一个ItemsControl,我用一个按钮添加任务。 如果我有一些任务,我想访问按下新按钮的特定项目(或行)。 我不知道如何访问此ItemControl中的每个项目。有优雅的方式吗?
目前我只能访问最后一项,因为它已保存在绑定中..
XAML:
<StackPanel Grid.Row="1" Grid.ColumnSpan="4" HorizontalAlignment="Left">
<ItemsControl ItemsSource="{Binding TaskList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="150" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="1"
Grid.ColumnSpan="3"
VerticalAlignment="Center"
FontSize="25"
Text="{Binding Name}" />
<Button
Height="30"
Width="30"
Margin="5"
Grid.Column="3"
Style="{DynamicResource RoundCorner}"
Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.GetNameCommand}" />
</Grid>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
XAML.CS:
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowView();
}
MainWindowView():
/// <summary>
/// Collection of tasks.
/// </summary>
public ObservableCollection<Task> TaskList { get; set; }
/// <summary>
/// Task class for the task list.
/// </summary>
public class Task
{
public string Name { get; set; }
public Task(string name)
{
Name = name;
}
public string GetTaskName()
{
string name = Name;
return name;
}
}
[...]
/// <summary>
/// Default constructor.
/// </summary>
public MainWindowView()
{
// Init properties.
MainWindowTitle = "Hello World";
MainWindowHeight = "130";
MainWindowWidth = "455";
TaskList = new ObservableCollection<Task>();
// Commands.
this.ResizeWindowCommand = new RelayCommand(ResizeWindowPlus);
this.TaskPlusCommand = new RelayCommand(TaskPlus);
this.GetNameCommand = new RelayCommand(GetName);
ResizeWindow();
}
[...]
/// <summary>
/// Adds a task to the list.
/// </summary>
public void TaskPlus()
{
// Get TaskName of TextInput.
TextInput txtÍnput = new TextInput();
txtÍnput.ShowDialog();
// Add the task to the list.
if (string.IsNullOrEmpty(txtÍnput.TaskName))
return;
TaskList.Add(new Task(txtÍnput.TaskName));
ResizeWindowPlus();
}
答案 0 :(得分:0)
如果要将按钮的相应列表项绑定到命令方法的参数,可以使用CommandParameter="{Bnding}"
:
<StackPanel Grid.Row="1" Grid.ColumnSpan="4" HorizontalAlignment="Left">
<ItemsControl ItemsSource="{Binding TaskList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="150" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="1"
Grid.ColumnSpan="3"
VerticalAlignment="Center"
FontSize="25"
Text="{Binding Name}" />
<!-- Here we add the CommandParameter -->
<Button
Height="30"
Width="30"
Margin="5"
Grid.Column="3"
Style="{DynamicResource RoundCorner}"
Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.GetNameCommand}"
CommandParameter="{Binding}" />
</Grid>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>