0

次のように宣言されたデータグリッドがあります。

<DataGrid AutoGenerateColumns="True" Margin="10,174,12,35" Name="dataGridArchiveQueue" Visibility="Visible" 
AlternatingRowBackground="#01000000" 
BorderBrush="#FF688CAF" 
HorizontalGridLinesBrush="#37000000" 
VerticalGridLinesBrush="#37000000" 
CanUserAddRows="False" 
CanUserDeleteRows="False" 
IsReadOnly="True" 
SelectedItem="{Binding SelectedItemArchiveGrid}" Grid.ColumnSpan="2">
     <DataGrid.Resources>
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                                         Color="LightBlue" />
     </DataGrid.Resources>
</DataGrid>

このデータグリッドに次のデータを入力します。

public DataView ArchiveEvents(string status, string sourceLibrary, string destinationLibrary)
        {
            var queryString = sourceLibrary == destinationLibrary &&
                                 (!String.IsNullOrEmpty(sourceLibrary) && !String.IsNullOrEmpty(destinationLibrary))
                                    ? @"SELECT  archive_queue_id AS 'ID',
                                                [status] AS 'Status' , 
                                                source_site AS 'Source Site', 
                                                source_url AS 'Source Library', 
                                                destination_site AS 'Destination Site' ,
                                                destination_url AS 'Destination Library',
                                                CASE WHEN recurring_job = 1
                                                THEN 'Yes'
                                                ELSE 'No'
                                                END AS 'Recurring Job' ,
                                                isnull(cast(last_run AS varchar(30)), 'Never') AS 'Last Run' ,
                                                isnull(cast(created_by AS varchar(30)), 'Unknown') AS 'Created By' ,
                                                created_date AS 'Created Date' ,
                                                isnull(last_modified_by, '') AS 'Last Modified By' ,
                                                isnull(cast(last_modified_date AS varchar(30)),'') AS 'Last Modified Date'
                                        FROM    marlin.archive_queue
                                        WHERE   upper([status]) LIKE '%' + upper(@status) + '%'
                                                AND upper(source_url) LIKE '%' + upper(@source_library) + '%'
                                                or upper(destination_url) LIKE '%' + upper(@destination_library) + '%'"
                                    : @"SELECT  archive_queue_id AS 'ID',
                                                [status] AS 'Status' , 
                                                source_site AS 'Source Site', 
                                                source_url AS 'Source Library', 
                                                destination_site AS 'Destination Site' ,
                                                destination_url AS 'Destination Library',
                                                CASE WHEN recurring_job = 1
                                                THEN 'Yes'
                                                ELSE 'No'
                                                END AS 'Recurring Job' ,
                                                isnull(cast(last_run AS varchar(30)), 'Never') AS 'Last Run' ,
                                                isnull(cast(created_by AS varchar(30)), 'Unknown') AS 'Created By' ,
                                                created_date AS 'Created Date' ,
                                                isnull(last_modified_by, '') AS 'Last Modified By' ,
                                                isnull(cast(last_modified_date AS varchar(30)),'') AS 'Last Modified Date'
                                        FROM    marlin.archive_queue
                                        WHERE   upper([status]) LIKE '%' + upper(@status) + '%'
                                                AND upper(source_url) LIKE '%' + upper(@source_library) + '%'
                                                AND upper(destination_url) LIKE '%' + upper(@destination_library) + '%'";


            using (var connection = new SqlConnection(GetConnectionString()))
            {
                using (var cmd = new SqlCommand(queryString, connection))
                {
                    connection.Open();

                    cmd.Parameters.AddWithValue("status", status != "Anything" ? status : "");
                    cmd.Parameters.AddWithValue("source_library", sourceLibrary != "Anything" ? sourceLibrary : "");
                    cmd.Parameters.AddWithValue("destination_library", destinationLibrary != "Anything" ? destinationLibrary : "");


                    using (var reader = cmd.ExecuteReader())
                    {
                        var dt = new DataTable();
                        dt.Load(reader);

                        connection.Close();
                        return dt.DefaultView;
                    }
                }
            }
        }

ボタンを使用して編集または削除できるように、選択した行の内容を取得しようとしています。これを行うために、私は次のプロパティを持っています:

public DataView SelectedItemArchiveGrid { get; set; }

ただし、現在選択されているアイテムの詳細を取得するためにこのプロパティにアクセスする方法がわかりません。

4

1 に答える 1

1

SelectedItemArchiveGridのバインディングプロパティタイプをDataViewからDataRowViewに変更する必要があり、バインディングにはXAMLでRelativeSourceを使用する必要があります

XAML

SelectedItem="{Binding DataContext.SelectedItemArchiveGrid, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}"

C#

private DataRowView _selectedItemArchiveGrid;
public DataRowView SelectedItemArchiveGrid
{
    get
    {
        return _selectedItemArchiveGrid;
    }
    set
    {
        _selectedItemArchiveGrid = value;
    }
}
于 2012-09-04T04:50:02.933 に答える