所以我试图为我的自定义控件添加一些“效果”。 我想在“IsPressed”上更改一些属性,但出于某种原因,当我尝试这样做时,我不断收到错误
“IsPressed”无法识别或无法访问。“
我不确定这是因为它是一个自定义控件,还是因为我错过了一些东西,我从来没有尝试过使用自定义控件,我只使用普通控件。 这样做的正确方法是什么?
所以我拥有的是这个
这根本不起作用,我不知道为什么
<UserControl.Resources>
<Style x:Key="UserControlStyle1" TargetType="{x:Type UserControl}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type UserControl}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
这将是控件
<UserControl x:Class="MasterViewDetail_MVVM.Controls.MasterView.MasterViewUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:designModel="clr-namespace:MasterViewDetail_MVVM.ViewModel.Nodes.DesignModel"
mc:Ignorable="d"
d:DesignHeight="60" d:DesignWidth="300">
<Grid Background="#2B2F42">
<Grid.ColumnDefinitions>
<!-- Column 0 -->
<!-- New SelectedItem Bar -->
<ColumnDefinition Width="Auto"/>
<!-- Column 1 -->
<!-- Color Circle (Blue Password, Green Note, Orange File) -->
<ColumnDefinition Width="Auto"/>
<!-- Column 2 -->
<!-- Main content -->
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- Selected Item Bar -->
<Border Background="#FFB400" Width="5"/>
<!-- Color Circle -->
<Border Grid.Column="1" Padding="8">
<!-- Data Type background -->
<Border Name="BackgroundRGB" Background="CornflowerBlue"
Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
CornerRadius="{Binding ActualHeight, RelativeSource={RelativeSource Self}}">
<!-- Data Type Text -->
<TextBlock Name="DataTypeText" Text="P"
VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White"
FontSize="20"
FontFamily="Levenim MT"/>
</Border>
</Border>
<!-- Main content -->
<!-- DONT FORGET TO NAME THE TEXTBLOCKS! -->
<Border Grid.Column="2" Padding="0 0 8 0">
<StackPanel VerticalAlignment="Center">
<!-- Title OR Username -->
<TextBlock Name="TitleText" Text="Happy@Some.com" FontWeight="Bold"
TextTrimming="CharacterEllipsis"
Foreground="#00AFE1"/>
<!-- Content -->
<TextBlock Name="ContentText" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ex tellus, rutrum quis eros in, bibendum posuere ligula.
Cras at dictum leo, in pellentesque nisi. Sed sollicitudin sagittis lacinia. Quisque sed nibh egestas, vehicula neque in, accumsan lectus. Ut quis eleifend nulla.
Aliquam orci nibh, elementum sit amet faucibus vitae, feugiat ac lacus. Suspendisse in posuere libero. Aenean eu condimentum sem. "
FontWeight="Light"
TextTrimming="CharacterEllipsis"
Foreground="Gray"
/>
</StackPanel>
</Border>
</Grid>
</UserControl>
修改
答案 0 :(得分:0)
仅从你的例子来看,我可以看到这个问题。您这样做了IsMouseOver
将Background
的{{1}}属性更改为红色。然后,您为UserControl
创建了ControlTemplate
,基本上覆盖了UserControl
的构建方式,并且您已经打破了#{1}}。属性与UserControl
本身的默认连接。
可以使用UserControl
解决此问题,但您错过了TemplateBinding
属性的TemplateBinding
。添加它,您的示例应该可以。