Вложенная Silverlight Datagrid - Детали строки работают отлично, но я хочу кнопку!

Возвратите Объект. Это позволяет Вам помещать дополнительную функциональность в Класс при необходимости в нем. Недолгие объекты в Java быстры, чтобы создать и собраться.

5
задан Dennis Ward 7 August 2009 в 01:53
поделиться

4 ответа

Деннис,

Если вы еще не нашли ответа на этот вопрос, я хотел такое же поведение и решил его, настроив шаблон RowHeaderTemplate, который позволяет вам добавить кнопку в заголовок. для каждой строки. Затем я реализовал обработчик для кнопки следующим образом:

private void ToggleButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
    ToggleButton button = sender as ToggleButton;
    DataGridRow row = button.GetVisualAncestorOfType<DataGridRow>();

    if (button.IsChecked == true)
    {
        row.DetailsVisibility = Visibility.Visible;

        //Hide any already expanded row. We only want one expanded at a time for simplicity and
        //because it masks a virtualization bug in the datagrid.
        if (_expandedRow != null)
            _expandedRow.DetailsVisibility = Visibility.Collapsed;

        _expandedRow = row;
    }
    else
    {
        row.DetailsVisibility = Visibility.Collapsed;
        _expandedRow = null;
    }
}

Обратите внимание, что GetVisualAncestorOfType <> - это метод расширения, который я реализовал, чтобы копаться в визуальном дереве.

Вам также необходимо установить свойство HeadersVisibility таблицы данных на Row или все

6
ответ дан 14 December 2019 в 01:13
поделиться

Я предлагаю вам взглянуть на событие RowVisibilityChanged в сетке данных, в основном, когда видимость строки изменится на "Visible", а затем загрузить информацию для строки.

0
ответ дан 14 December 2019 в 01:13
поделиться

here is another way to achieve what you are trying to do:

In the DataGrid set up a LoadingRow Event like this:

<data:DataGrid LoadingRow="ItemsGrid_LoadingRow" .....

In the DataGrid create a Template Column which will contain a Button such as the following:

<data:DataGridTemplateColumn CellStyle="{StaticResource DataGridCellStyle1}" CanUserReorder="False">
       <data:DataGridTemplateColumn.CellTemplate>
             <DataTemplate>
                    <Button x:Name="ViewButton" Click="ToggleRowDetailsVisibility" Cursor="Hand" Content="View Details" />                                       
             </DataTemplate>
         </data:DataGridTemplateColumn.CellTemplate>
       </data:DataGridTemplateColumn>

In the LoadingRow Event locate the button that is (in this case) stored in the first column of the DataGrid, then store the current DataGridRow into the buttons Tag element

private void ItemsGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        var ViewButton = (Button)ItemsGrid.Columns[0].GetCellContent(e.Row).FindName("ViewButton");
        ViewButton.Tag = e.Row;
    }

In the Buttons EventHandler (in this case ToggleRowDetailsVisibility) we will extract the Row so that we can toggle its DetailsVisibility

In the LoadingRow Event locate the button that is (in this case) stored in the first column of the DataGrid, then store the current DataGridRow into the buttons Tag element

private void ToggleRowDetailsVisibility(object sender, RoutedEventArgs e)
    {
        var Button = sender as Button;
        var Row = Button.Tag as DataGridRow;

        if(Row != null)
        {
            if(Row.DetailsVisibility == Visibility.Collapsed)
            {
                Row.DetailsVisibility = Visibility.Visible;

                //Hide any already expanded row. We only want one expanded at a time for simplicity and
                //because it masks a virtualization bug in the datagrid.
                if (CurrentlyExpandedRow != null)
                {
                    CurrentlyExpandedRow.DetailsVisibility = Visibility.Collapsed;
                }

                CurrentlyExpandedRow = Row;
            }
            else
            {
                Row.DetailsVisibility = Visibility.Collapsed;
                CurrentlyExpandedRow = null;
            }
        }
    }

You will notice that "CurrentlyExpandedRow", this is a Global variable, of type DataGridRow, that we store the currently expanded row in, this allows us to close that Row when a new one is to be opened.

Hope this helps.

1
ответ дан 14 December 2019 в 01:13
поделиться

В дополнение к ответу uxrx здесь приведен код поиска предка

public static partial class Extensions 
{ 
    public static T FindAncestor<T>(DependencyObject obj) where T : DependencyObject 
    { 
        while (obj != null) 
        { 
            T o = obj as T; 
            if (o != null)            
                return o; 

            obj = VisualTreeHelper.GetParent(obj); 
        } 
        return null; 
    } 

    public static T FindAncestor<T>(this UIElement obj) where T : UIElement 
    { 
        return FindAncestor<T>((DependencyObject)obj); 
    } 
}
1
ответ дан 14 December 2019 в 01:13
поделиться
Другие вопросы по тегам:

Похожие вопросы: