У меня есть datagrid, в одном из столбцов я хочу отобразить строку - в каждой ячейке в столбце есть другая строка (которую я получаю от пользователя), которая выглядит так: «data1, data2, data3, .. . »и сделать каждый datai (data1, data2, ...) кликабельным. как я могу это достичь?Как отобразить интерактивный список вещей в ячейке datagrid?
0
A
ответ
0
Вот отредактированная версия:
Xaml (положить его внутрь
<DataGridTemplateColumn.CellTemplate
):<DataTemplate DataType="{x:Type soDataGridHeplAttempt:ClicableItemsModel}"> <ListBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding ClickableItems}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"></StackPanel> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <Button Width="70" Content="{Binding }" Style="{StaticResource ButtonInCellStyle}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Command}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}"/> </DataTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> </ListBox> </DataTemplate>
XAML ресурсы использовать, если вы хотите изменить содержимое кнопки:
<Style x:Key="ButtonInCellStyle" TargetType="Button"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <TextBox Background="{x:Null}" MaxWidth="40" BorderBrush="{x:Null}" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=Content, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" HorizontalAlignment="Center" VerticalAlignment="Center" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Text}"> </TextBox> </DataTemplate> </Setter.Value> </Setter>
ViewModel:
private ICommand _command; public ObservableCollection<ClicableItemsModel> Strings { get; set; } public ICommand Command { get { return _command ?? (_command = new RelayCommand<object>(MethodOnCommmand)); } } private void MethodOnCommmand(object obj) { }
код Clickable Элементы модели (создать класс ClicableItemsModel его туда):
public ObservableCollection<String> ClickableItems { get; set; }
С уважением,
@stacker wait Я немного отредактирую это. – Ilan
Я хочу иметь (в той же ячейке) более 1 clickable item. – Stacker
@Stacker попробует версию. – Ilan