2011-08-12 39 views
1

這好像應該是簡單的,但我不能讓它發生......我有一個模板列,其中包括一個按鈕一個DataGrid:按鈕風格

<DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
      <Button Style="{StaticResource LinkButton}" Content="{Binding Path=...}"/> 
     </StackPanel> 
    </DataTemplate> 
</DataGridTemplateColumn.CellTemplate> 

和我的LinkBut​​ton的風格是這樣的:

<Style x:Key="LinkButton" TargetType="Button"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <ControlTemplate.Resources> 
        <Style TargetType="{x:Type TextBlock}"> 
         <Setter Property="TextDecorations" Value="Underline" /> 
        </Style> 
       </ControlTemplate.Resources> 
       <ContentPresenter /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Foreground" Value="Blue" /> 
    <Setter Property="Cursor" Value="Hand" /> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="true"> 
      <Setter Property="Foreground" Value="Red" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

當選擇該行,我想LinkBut​​ton的的前景顏色更改爲白色或東西。有沒有簡單的方法來完成這個?

回答

4
<Style.Triggers> 
    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" 
       Value="True"> 
      <Setter Property="TextElement.Foreground" Value="White"/> 
    </DataTrigger> 
    <!-- Here be your other trigger, order matters, if it is the other way around the above trigger overrides your mouse-over trigger --> 
</Style.Triggers> 

或者類似的東西...

順便說一句,怎麼樣使用DataGridHyperlinkColumn,或者至少是隻是一個普通的Hyperlink而不是Button)的

+0

神奇, 非常感謝。還有其他的東西在網格和列中強制我使用模板列。我在設計時嘗試了超鏈接,但無法使其工作。我認爲這是因爲我需要發送一個命令給視圖模型,在這一點上我不記得了。 – drowned