我定义了以下资源:
<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}">
<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>
但不幸的是,StyleTriggers并没有按照我的想法行事。不透明度已更改,但背景仍然相同。我也尝试过只有一个setter,但背景仍未改变:
<Style.Triggers>
<Trigger Property="Tag" Value="DragEnter">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
这里有什么问题?
=== EDIT ===
这里是我用来获取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;
}
答案 0 :(得分:1)
样式无法设置背景,因为此属性与不透明度不同,在FrameworkElement类中不存在。 Framework元素的属性可以直接使用,但是Control的属性(例如Background,BorderThickness,HorizontalContentAlignment)应该在模板中定义。
这是一个正确的版本,我添加了一个带背景的边框:
<ControlTemplate TargetType="{x:Type ContentControl}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}">
</ContentPresenter>
</Border>
</ControlTemplate>