2014-04-16 117 views
0

enter image description here你好,我有一些WPF中的DataGrid風格的問題。我有一個由圖像按鈕(內容爲圖像的按鈕)組成的列,表示在Excel中打開文件。該圖像僅出現在第一個創建的行上,而沒有圖像的情況下,該列中的其餘按鈕不變。WPF DataGrid:如何讓DataGridTemplateColumn中的所有按鈕具有相同的圖像?

問題:如何讓「Open」列中的每個按鈕都具有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> 

任何幫助將不勝感激。

問候,

凱爾

回答

0

我找到了一個解決方案:

從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> 
相關問題