如何在 UI 中单击 ObservableCollection 中的对象?
How to get object from ObservableCollection within clicking it in UI?
我有一个 TextBlocks 字段,当我单击一个命令触发器时,它显示在统一网格中。我现在如何从显示的集合中获取被点击的特定对象?
有关更多信息,请查看我之前关于文本块命令的问题:
XAML:
<ItemsControl ItemsSource="{Binding CellCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Background="{Binding Path=CellBackgroundColor}">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" MouseAction="LeftClick"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Grid.Row="0" Rows="25" Columns="25">
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
视图模型:
public ObservableCollection<Cell> CellCollection { get; set; }
public MainWindowViewModel()
{
StartCommand = new RelayCommand(Start);
StopCommand = new RelayCommand(Stop);
TestCommand = new RelayCommand(Test);
CellCollection = new ObservableCollection<Cell>();
//begins with 1 not 0
//generates 625 cells for the field (25x25)
for (int iRow = 1; iRow < 26; iRow++)
{
for (int iColumn = 1; iColumn < 26; iColumn++)
{
CellCollection.Add(new Cell() { Row = iRow, Column = iColumn, IsAlive = false, CellBackgroundColor = new SolidColorBrush(Colors.Transparent) });
}
}
}
我尝试了以下操作但没有成功:
<MouseBinding CommandParameter="{Binding CTest}" Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" MouseAction="LeftClick"/>
属性
Cell _CTest;
public Cell CTest
{
get
{
return _CTest;
}
set
{
if(_CTest != value)
{
_CTest = value;
RaisePropertyChanged("CTest");
}
}
}
void Test(object parameter)
{
Cell test = CTest;
}
将 MouseBinding 中的 CommandParameter 直接绑定到 DataTemplate 项:
<MouseBinding
Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"
MouseAction="LeftClick"/>
命令方法将通过参数接收它:
void Test(object parameter)
{
Cell test = (Cell)parameter;
}
我有一个 TextBlocks 字段,当我单击一个命令触发器时,它显示在统一网格中。我现在如何从显示的集合中获取被点击的特定对象?
有关更多信息,请查看我之前关于文本块命令的问题:
XAML:
<ItemsControl ItemsSource="{Binding CellCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Background="{Binding Path=CellBackgroundColor}">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" MouseAction="LeftClick"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Grid.Row="0" Rows="25" Columns="25">
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
视图模型:
public ObservableCollection<Cell> CellCollection { get; set; }
public MainWindowViewModel()
{
StartCommand = new RelayCommand(Start);
StopCommand = new RelayCommand(Stop);
TestCommand = new RelayCommand(Test);
CellCollection = new ObservableCollection<Cell>();
//begins with 1 not 0
//generates 625 cells for the field (25x25)
for (int iRow = 1; iRow < 26; iRow++)
{
for (int iColumn = 1; iColumn < 26; iColumn++)
{
CellCollection.Add(new Cell() { Row = iRow, Column = iColumn, IsAlive = false, CellBackgroundColor = new SolidColorBrush(Colors.Transparent) });
}
}
}
我尝试了以下操作但没有成功:
<MouseBinding CommandParameter="{Binding CTest}" Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" MouseAction="LeftClick"/>
属性
Cell _CTest;
public Cell CTest
{
get
{
return _CTest;
}
set
{
if(_CTest != value)
{
_CTest = value;
RaisePropertyChanged("CTest");
}
}
}
void Test(object parameter)
{
Cell test = CTest;
}
将 MouseBinding 中的 CommandParameter 直接绑定到 DataTemplate 项:
<MouseBinding
Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"
MouseAction="LeftClick"/>
命令方法将通过参数接收它:
void Test(object parameter)
{
Cell test = (Cell)parameter;
}