1
我有DataGridCell
一個Style
DatagridCell風格
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Foreground" Value="{StaticResource ForegroundBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" SnapsToDevicePixels="True" >
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Margin="4, 0, 0, 0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{StaticResource DarkForegroundBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
它工作時,我是這樣定義的列:
<DataGridTemplateColumn Header="Column1" Width="Auto" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Property1, Mode=OneWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
但是,當我有TextBox
而不是TextBlock
這樣的:
<DataGridTemplateColumn Width="Auto" Header="Column1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Property1, Mode=OneWay}" IsReadOnly="True" TextWrapping="Wrap" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
它不起作用,因爲TextBox
有它自己的Style
。 我需要使用TextBox
,因爲我想讓用戶從單元格中選擇文本。但是當我選擇單元格/行時,我也需要更改Foreground
顏色。
(背景顏色是深顏色和前景顏色是輕,但在選擇了小區/行,則背景顏色爲淡和前景顏色應該是暗的)
EDIT
我編輯我的問題更清楚。對不起以前的誤解。我的目標是TextBox
在DataGridCell
中,並使用Trigger
來自DataGridCellStyle
。
任何幫助表示讚賞。
真是對不起前景。也許我沒有足夠的自我解釋......設置文本框背景爲透明沒有問題......真正的問題是,文本框仍然有自己的前景,而不是單元格的前景......所以當行是選擇。請參閱DataGridCellStyle中的那些觸發器 – 2013-03-21 15:58:26
我編輯了我的問題以便更清楚。謝謝。 – 2013-03-21 18:58:24
明白了。固定。我剛剛在一個小時前的另一個問題上解決了這個相同的問題。 – 2013-03-22 17:10:58