我创建了一个从Canvas继承的自定义Canvas,我声明了一个新的Dependency属性“ NewMouseOver”,希望通过触发器中的Setter对其进行影响。
public class CanvaNetwork : Canvas
{
public CanvaNetwork() { }
public bool NewMouseOver
{
get { return (bool)GetValue(NewMouseOverProperty); }
set { SetValue(NewMouseOverProperty, value); }
}
public static readonly DependencyProperty NewMouseOverProperty =
DependencyProperty.Register("NewMouseOver", typeof(bool),
typeof(CanvaNetwork), new PropertyMetadata(false));
}
这是我的XAML:
<DataTemplate DataType="{x:Type local:Node}">
<local:CanvaNetwork x:Name="ItemCanvas_Node"
NewMouseOver="{Binding MyMouseOver}"
Background="Transparent">
<Path x:Name="Path_NodeProcess"
Stroke="Green"
Fill="Gray"
Stretch="None"
Data="{Binding Path =Geometryform}"
Visibility="{Binding Path=Visibility}">
</Path>
<local:CanvaNetwork.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="NewMouseOver" Value="True" />
</Trigger>
</local:CanvaNetwork.Triggers>
</local:CanvaNetwork>
</DataTemplate>
hera是我的Node类:
Public Node :DependencyObject
{
public static readonly DependencyProperty MyMouseOverProperty =
DependencyProperty.Register("MyMouseOver", typeof(bool), typeof(NodeProcess), new PropertyMetadata(true,new PropertyChangedCallback(On_MyMouseOver)));
private static void On_MyMouseOver(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//..some code
}
public bool MyMouseOver
{
get { return (bool)GetValue(MyMouseOverProperty); }
set { SetValue(MyMouseOverProperty, value); }
}
}
我想要的是: 1-i具有DependencyProperty:NewMouseOver(具有和设置不像原始Canvas类中的IsMouseOver)。 通过触发器/设置器2-访问NewMouseOver并更改NewMouseOver的状态。 3-via XAML:将绑定绑定设置为:NewMouseOver(在CanvaNetwork中)和MyMouseOver(在Node类中) 4之后,我将使用On_MyMouseOver(位于Node类中)和MyMouseOver来制作一些东西。
答案 0 :(得分:0)
我认为我可以回答有关如何更新画布对象中的DependencyProperty的问题。
要测试它,我将为依赖项属性定义一个“ on change”方法。您可以在此处放置一个断点以验证是否已设置依赖项属性。
class CanvaNetwork : Canvas
{
public CanvaNetwork ( ) { }
public static readonly DependencyProperty NewMouseOverProperty
= DependencyProperty.Register ( "NewMouseOver",
typeof (bool),
typeof (CanvaNetwork),
new PropertyMetadata (false, OnNewMouseOverChanged)) ;
public bool NewMouseOver
{
get { return (bool)GetValue (NewMouseOverProperty); }
set { SetValue (NewMouseOverProperty, value); }
}
public static void OnNewMouseOverChanged ( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
}
}
在DataTemplate中,您必须在样式中定义触发器。
<DataTemplate DataType="{x:Type local:Node}">
<local:CanvaNetwork x:Name="ItemCanvas_Node"
Background="red" Height="100" Width="100">
<Path x:Name="Path_NodeProcess"
Stroke="Green"
Fill="Gray"
Stretch="None"
Data="{Binding Path =Geometryform}"
Visibility="{Binding Path=Visibility}">
</Path>
<local:CanvaNetwork.Style>
<Style>
<Setter Property="local:CanvaNetwork.NewMouseOver" Value="False" />
<Style.Triggers>
<Trigger Property="Canvas.IsMouseOver" Value="True">
<Setter Property="local:CanvaNetwork.NewMouseOver" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</local:CanvaNetwork.Style>
</local:CanvaNetwork>
</DataTemplate>
如果样式中设置了默认属性,则只能使用触发器更新属性。
为此,我删除了您的属性 NewMouseOver =“ {Binding MyMouseOver}” 。从您的列表中,第1点和第2点起作用,但是删除此属性意味着第3点不起作用。
但是,我认为您可能还是采取了错误的方法。将MouseOver事件连接到Node类中的command属性不是更好,如下所述:
答案 1 :(得分:0)
我的xaml变成这样:
<local:CanvaNetwork.Style>
<Style TargetType="{x:Type local:CanvaNetwork}">
<Setter Property="NewMouseOver" Value="False"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="NewMouseOver" Value="True"/>
<Setter Property="Cursor" Value="Cross"/>
</Trigger>
</Style.Triggers>
</Style>
</local:CanvaNetwork.Style>
</local:CanvaNetwork>
我删除了`NewMouseOver =“ {Binding MyMouseOver}”
但是添加了这个:
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=ItemCanvas_Node, Path=NewMouseOver}" Value="True">
<Setter Property="{Binding MyMouseOver}" Value="True"/>
</DataTrigger>
</DataTemplate.Triggers>
在添加DataTrigger代码之前,光标变成十字形,在该位置上我的“ MyMouseOver”设置程序上出现错误,它必须是依赖项属性...但这是一个依赖项属性!