WPF DataGrid: как сделать так, чтобы все кнопки в DataGridTemplateColumn имели одинаковое изображение?

введите здесь описание изображенияЗдравствуйте, у меня возникли проблемы со стилем DataGrid в WPF. У меня есть столбец, состоящий из кнопки изображения (кнопка, где содержимое является изображением), которая представляет собой открытие файла в Excel. Изображение появляется только для первой созданной строки, а остальные кнопки в столбце остаются без изображения.

Вопрос: как я могу заставить каждую кнопку в столбце «Открыть» иметь изображение Excel в качестве содержимого?

Стиль кнопки:

    <Style x:Key="btnOpenJr" TargetType="Button">
        <Setter Property="Width" Value="20"/>
        <Setter Property="Height" Value="20"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="Content">
            <Setter.Value>
                <Image Height="20" Source="/BTLogFrontEnd;component/Resources/open_excel.ico"/>
            </Setter.Value>
        </Setter>
    </Style>

Стиль DataGridCell:

    <Style x:Key="cellStyle" TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="False">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Grid Background="DeepSkyBlue">
                                <ContentPresenter
                                    VerticalAlignment="Center" 
                                    HorizontalAlignment="Center"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Grid Background="Lime">
                                <ContentPresenter
                                    VerticalAlignment="Center" 
                                    HorizontalAlignment="Center"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

Определение DataGrid:

    <DataGrid
        Name="grdData"
        CellStyle="{StaticResource cellStyle}"
        Margin="10"
        FontFamily="Verdana"
        Foreground="MidnightBlue"
        Background="MidnightBlue"
        BorderBrush="Transparent"
        AutoGenerateColumns="False"
        RowHeight="22"
        CanUserAddRows="False"
        CanUserDeleteRows="False"
        CanUserReorderColumns="True"
        CanUserResizeColumns="False"
        CanUserResizeRows="False"
        CanUserSortColumns="True"
        RowHeaderWidth="5"
        ItemsSource="{Binding GridData}"
        SelectionUnit="CellOrRowHeader"
        Width="550"
        ColumnHeaderStyle="{StaticResource gridHeaderStyle}"
        SelectionMode="Extended">
        <DataGrid.Columns>
            <DataGridTemplateColumn
                Width="40"
                Header="Open">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Style="{StaticResource btnOpenJr}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridCheckBoxColumn
                Width="65"
                Header="Approved">                  
            </DataGridCheckBoxColumn>
            <DataGridTextColumn
                Width="40"
                Binding="{Binding Number}"
                Header="JR #"/>
            <DataGridTextColumn
                Width="55"
                Binding="{Binding Process}"
                Header="Process"/>
            <DataGridTextColumn
                Width="55"
                Binding="{Binding Wafer}"
                Header="Wafer"/>
            <DataGridTextColumn
                Width="335"
                Binding="{Binding Description}"
                Header="Description"/>
        </DataGrid.Columns>
    </DataGrid>

Любая помощь будет оценена.

С уважением,

Кайл


person kformeck    schedule 16.04.2014    source источник


Ответы (1)


Я нашел решение:

Удалите стиль кнопки "btnOpenJr" из файла app.xaml и используйте встроенный стиль для этого столбца в определении DataGrid.

Новое определение DataGrid становится следующим:

    <DataGrid
        Name="grdData"
        CellStyle="{StaticResource cellStyle}"
        Margin="10"
        FontFamily="Verdana"
        Foreground="MidnightBlue"
        Background="MidnightBlue"
        BorderBrush="Transparent"
        AutoGenerateColumns="False"
        RowHeight="22"
        CanUserAddRows="False"
        CanUserDeleteRows="False"
        CanUserReorderColumns="True"
        CanUserResizeColumns="False"
        CanUserResizeRows="False"
        CanUserSortColumns="True"
        RowHeaderWidth="5"
        ItemsSource="{Binding GridData}"
        SelectionUnit="CellOrRowHeader"
        Width="550"
        ColumnHeaderStyle="{StaticResource gridHeaderStyle}"
        SelectionMode="Extended">
        <DataGrid.Columns>
            <DataGridTemplateColumn
                Width="40"
                Header="Open">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button 
                            Width="20"
                            Height="20"
                            HorizontalContentAlignment="Center">
                            <Image
                                Height="20"
                                Source="/BTLogFrontEnd;component/Resources/open_excel.ico"/>
                        </Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridCheckBoxColumn
                Width="65"
                Header="Approved">                  
            </DataGridCheckBoxColumn>
            <DataGridTextColumn
                Width="40"
                Binding="{Binding Number}"
                Header="JR #"/>
            <DataGridTextColumn
                Width="55"
                Binding="{Binding Process}"
                Header="Process"/>
            <DataGridTextColumn
                Width="55"
                Binding="{Binding Wafer}"
                Header="Wafer"/>
            <DataGridTextColumn
                Width="335"
                Binding="{Binding Description}"
                Header="Description"/>
        </DataGrid.Columns>
    </DataGrid>
person kformeck    schedule 16.04.2014