2015-10-20 2 views
0

У меня есть datagrid, в одном из столбцов я хочу отобразить строку - в каждой ячейке в столбце есть другая строка (которую я получаю от пользователя), которая выглядит так: «data1, data2, data3, .. . »и сделать каждый datai (data1, data2, ...) кликабельным. как я могу это достичь?Как отобразить интерактивный список вещей в ячейке datagrid?

ответ

0

Вот отредактированная версия:

  1. 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> 
    
  2. 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> 
    

  3. 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) 
    { 
    
    } 
    
  4. код Clickable Элементы модели (создать класс ClicableItemsModel его туда):

    public ObservableCollection<String> ClickableItems { get; set; } 
    
  5. при работе: enter image description here

С уважением,

+0

@stacker wait Я немного отредактирую это. – Ilan

+0

Я хочу иметь (в той же ячейке) более 1 clickable item. – Stacker

+0

@Stacker попробует версию. – Ilan