是否可以使用DataGridTextColumn绑定图像?如果是的话怎么做?

时间:2015-11-24 06:19:17

标签: wpf

我想使用 DataGridTextColumn 为wpf datagrid绑定图像。 我在一个网格中使用 DataGridTextColumn DataGridTemplateColumn 。 我能够显示图像,但无法获得精确的SelectedIndex。

我将代码放在

下面
<DataGrid Name="dgNewBill" Margin="2,0,0,0" BorderThickness="0" HeadersVisibility="None" IsReadOnly="True" AutoGenerateColumns="False" GridLinesVisibility="Horizontal" Background="White" Cursor="Hand">                                               
                                            <DataGrid.Resources>
                                                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DarkTurquoise"/>
                                                <Style TargetType="{x:Type DataGridRow}">
                                                    <Style.Triggers>
                                                        <Trigger Property="IsMouseOver" Value="true">
                                                            <Setter Property="Foreground" Value="White" />
                                                            <Setter Property="Background" Value="DarkTurquoise" />
                                                            <Setter Property="FontSize" Value="14"></Setter>
                                                            <Setter Property="FontWeight" Value="Medium"></Setter>
                                                        </Trigger>

                                                        <Trigger Property="IsSelected" Value="true">
                                                            <Setter Property="Foreground" Value="White" />
                                                            <Setter Property="Background" Value="DarkTurquoise" />                                                                
                                                            <Setter Property="FontSize" Value="14"></Setter>
                                                            <Setter Property="FontWeight" Value="Medium"></Setter>
                                                        </Trigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </DataGrid.Resources>                                              

                                            <DataGrid.Columns>
                                                <DataGridTextColumn Header="SNumber"  Width="80" Binding="{Binding SNumber }">
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style TargetType="TextBlock">
                                                            <Setter Property="HorizontalAlignment" Value="Center" />  
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                                <DataGridTextColumn Header="Value 1"  Width="100" Binding="{Binding skuId, StringFormat={}{0:C}}">
                                                    <DataGridTextColumn.CellStyle>
                                                        <Style TargetType="DataGridCell">
                                                            <EventSetter Event="MouseLeftButtonDown" Handler="OpenSelectedItemDetails" />
                                                            <Setter Property="HorizontalAlignment" Value="Center" />
                                                        </Style>
                                                    </DataGridTextColumn.CellStyle>
                                                </DataGridTextColumn>
                                                <DataGridTextColumn Header="Description" Width="260" Binding="{Binding description }">
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style TargetType="TextBlock">
                                                            <Setter Property="HorizontalAlignment" Value="Left" />
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                                <DataGridTextColumn Header="Quantity" Width="120" Binding="{Binding quantity }">
                                                    <DataGridTextColumn.CellStyle>
                                                        <Style TargetType="DataGridCell">
                                                            <EventSetter Event="MouseLeftButtonDown" Handler="OpenSelectedItemQuantity" />
                                                            <Setter Property="HorizontalAlignment" Value="Center" />
                                                        </Style>
                                                    </DataGridTextColumn.CellStyle>
                                                </DataGridTextColumn>
                                                <DataGridTextColumn Header="Price" Width="70" Binding="{Binding price, StringFormat={}{0:#.00}}">
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style TargetType="TextBlock">
                                                            <EventSetter Event="MouseLeftButtonDown" Handler="OpenSelectedItemPriceEdit" />
                                                            <Setter Property="HorizontalAlignment" Value="Right" />
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                                <DataGridTextColumn Header="CartToral" Width="90" Binding="{Binding cartTotal, StringFormat={}{0:#.00}}">
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style TargetType="TextBlock">
                                                            <Setter Property="HorizontalAlignment" Value="Right" />
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>

                                                <DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True">
                                                    <DataGridTemplateColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <Image Height="20" Source="{Binding Image}" MouseLeftButtonDown="VoidSelectedItem" />
                                                        </DataTemplate>
                                                    </DataGridTemplateColumn.CellTemplate>
                                                </DataGridTemplateColumn>
                                            </DataGrid.Columns>
                                        </DataGrid>

1 个答案:

答案 0 :(得分:2)

您应该使用glDisable ( GL_LIGHTING ); glEnable(GL_TEXTURE_2D); glGenTextures(1, &glTextureObject); // set the current texture to the one just created glBindTexture (GL_TEXTURE_2D, glTextureObject); // repeat the texture in both directions glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // use bi-linear filtering on the texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE); // load the texture data into the graphics hardware. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 代替。这里有代码示例。

DataGridTemplateColumn

为其定义模板。

<DataGrid.Columns>
    <DataGridTemplateColumn Header="Image" CellTemplate="{StaticResource ImageCellTemplate}"/>
</DataGrid.Columns>