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 回答