使用DataContext& amp;将图像绑定到WPF Datagrid。 DataTable DefaultView

时间:2013-11-25 12:43:37

标签: c# wpf datagrid datatable

我使用下面的xaml代码来绑定wpf数据网格数据。

<DataGrid Height="560" x:Name="dgTest1" VerticalAlignment="Top" ColumnHeaderHeight="41" CanUserResizeRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False" HorizontalAlignment="Stretch" CanUserDeleteRows="False" CanUserAddRows="False" VerticalContentAlignment="Center" FontSize="13" FontFamily="Arial" GridLinesVisibility="Horizontal" HorizontalGridLinesBrush="#FFB2ACAC"  ScrollViewer.CanContentScroll="False" RowHeight="25" ItemsSource="{Binding}" Visibility="Visible" AlternatingRowBackground="WhiteSmoke" AlternationCount="1" IsReadOnly="True" SelectionUnit="FullRow">
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="BorderThickness" Value="0"/>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightSteelBlue"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
            </DataGrid.Resources>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Background" Value="LightGray"/>
                    <Setter Property="Foreground" Value="Black" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
        </DataGrid>

&安培;我将这个数据网格的值绑定在代码后面,例如

void CreateDataTable()
    {
        try
        {

        DataTable dtGlossary = new DataTable();
        DataColumn dcIcon1 = new DataColumn("Icon1");


        //dcIcon1.DataType = System.Type.GetType("System.Drawing.Bitmap");
        dcIcon1.AllowDBNull = true;

        dtGlossary.Columns.Add(dcIcon1);
        dtGlossary.Columns.Add("Desc1");

        DataColumn dcIcon2 = new DataColumn("Icon2");
        //dcIcon2.DataType = System.Type.GetType("System.Drawing.Bitmap");
        dcIcon2.AllowDBNull = true;

        dtGlossary.Columns.Add(dcIcon2);
        dtGlossary.Columns.Add("Desc2");

            AddNewRow(ref dtGlossary, global::Project1.Properties.Resources.play, "Playback a Test Case", global::MMTAS_WPF.Properties.Resources.abortedBadge, "Test Case Was Aborted");
            AddNewRow(ref dtGlossary, global::Project1.Properties.Resources.record_disabled, "Recording Disabled During Playback", global::MMTAS_WPF.Properties.Resources.willNotRunBadge, "Test Case Will Not Run");                
            dgTest1.DataContext = dtGlossary.DefaultView;
        }
        catch
        {

        }            
    }

 private void AddNewRow(ref DataTable pDataTable, System.Drawing.Bitmap pIcon1, string pDesc1, System.Drawing.Bitmap pIcon2, string pDesc2)
    {
        DataRow dr = pDataTable.NewRow();
        dr["Icon1"] = pIcon1;
        dr["Desc1"] = pDesc1;
        dr["Icon2"] = pIcon2;
        dr["Desc2"] = pDesc2;
        pDataTable.Rows.Add(dr);
        pDataTable.AcceptChanges();
    }

但是,图像没有绑定到datagrid,输出显示如, Output

我想使用DataTable将图像显示到Datagrid列。 我该如何解决这个问题? 提前谢谢。

0 个答案:

没有答案