开发者

WPF problems with trigger syntax

开发者 https://www.devze.com 2022-12-09 18:09 出处:网络
I can\'t get the following to work. The goal is to change the ZIndex of the usercontrol when the mouse is over its content.

I can't get the following to work. The goal is to change the ZIndex of the usercontrol when the mouse is over its content.

Using a simple property like "Background" instead of ZIndex does not work either. The compiler complains about "Value 'Grid.IsMouseOver' cannot be assigned to property 'Property'. Object reference not set to an instance of an object." (After compiling and starting the project).

Can someone please provide a working example of a trigger which changes some properties of a different control?

<UserControl x:Class="ImageToolWPF.Controls.sample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">

    <UserControl.Triggers>
        <Trigger SourceName="viewPort" Property="Grid.IsMouseOver" Value="True">
            <Setter TargetName="me" Property="UserControl.Panel.ZIndex" Value="2" />
        </Trigger>
    </UserControl.Triggers>

    <Border Name="border" CornerRadius="3,3,3,3" BorderThickness="3" BorderBrush="Green">
        <Grid Name="viewPort">
            <Label Name="labelTop" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" Background="#a0ffffff" Padding="4"/>
        </Grid&g开发者_高级运维t;
    </Border>
</UserControl>


There are a number of issues here:

  • FrameworkElement.Triggers can contain only EventTriggers, not general triggers. (See the Remarks in the MSDN docs.) You're going to need to move your trigger into a Style instead.
  • In your Setter, you've specified a TargetName of "me", but there doesn't appear to be any element with that name. I think you mean for the setter to affect the UserControl itself. In that case, if you move the Trigger into a Style on the UserControl, you can just omit the TargetName altogether: setters in a style automatically affect the styled element.
  • In your Setter, you've specified a Property of UserControl.Panel. That means you are expecting the target (the thing called "me") to have a property called UserControl, and that to have a property called Panel. I think what you are looking for is "(Panel.ZIndex)" - note the brackets showing that this is an attached property name rather than a multipart path, and that there is no UserControl prefix.
0

精彩评论

暂无评论...
验证码 换一张
取 消