繁体   English   中英

WPF DataGrid:如何使DataGridTemplateColumn中的所有按钮具有相同的图像?

[英]WPF DataGrid: How do I get all buttons in a DataGridTemplateColumn to have the same image?

在此处输入图片说明 您好,我在WPF中设置DataGrid样式时遇到一些问题。 我有一列,由代表在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>

任何帮助将不胜感激。

问候,

凯尔

我找到了解决方案:

从app.xaml文件中删除“ btnOpenJr”按钮样式,并为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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM