How do StyleTriggers work?
I defined the following resources:
<DataTemplate x:Key="DragTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label x:Name="DraggingSourceLabel" Content="{Binding Name}" BorderThickness="2" BorderBrush="White" Foreground="White" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Row="0" Grid.Column="0" FontSize="20"></Label>
</Grid>
</DataTemplate>
<Style x:Key="CursorStyle" TargetType="{x:Type ContentControl}">
<Setter Property="Opacity" Value="0.50"/>
<Setter Property="Background" Value"Black"/>
<Setter Property="ContentTemplate" Value="{StaticResource DragTemplate}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
开发者_Python百科 <ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}">
</ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Tag" Value="DragEnter">
<Setter Property="Opacity" Value="1.0"/>
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</s:SurfaceWindow.Resources>
But unfortunately, the StyleTriggers aren't working how I thought. The Opacity is changed, but the Background is still the same. I also tried it with only one setter, but the background still didn't changed:
<Style.Triggers>
<Trigger Property="Tag" Value="DragEnter">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
What's the problem here?
===EDIT===
Here the code I use to get the ContentControl:
ContentControl cursorVisual = new ContentControl()
{
Content = data,
Style = window.FindResource("CursorStyle") as Style
};
List<InputDevice> devices = new List<InputDevice>();
devices.Add(e.Contact);
ItemsControl dragSource = ItemsControl.ItemsControlFromItemContainer(draggedElement);
bool startDragOkay = SurfaceDragDrop.BeginDragDrop(sender as Grid, draggedElement, cursorVisual, data, devices, DragDropEffects.Move);
if (startDragOkay)
{
e.Handled = true;
//draggedElement.Visibility = Visibility.Hidden;
}
Style can't set Background because this property, unlike Opacity, isn't exist in a FrameworkElement class. Properties of a Framework element may be used out of the box, but properties of a Control (such as Background, BorderThickness, HorizontalContentAlignment) should be defined inside a template.
Here is a correct version, I've added a border with background:
<ControlTemplate TargetType="{x:Type ContentControl}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}">
</ContentPresenter>
</Border>
</ControlTemplate>
精彩评论