依赖属性不会触发

时间:2016-07-04 15:09:42

标签: wpf xaml dependency-properties

我遇到依赖属性问题。我一直在搜索“如何”,但到目前为止没有什么可以帮助我。

当我将某些内容拖入其中时,我需要一个网格来更改其背景颜色。到目前为止,这是我的代码:

VB

Friend Shared ReadOnly isDragOverPropertyKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("isDragOver", GetType(Boolean), _
                                                                                                      GetType(ucPageControl), _
                                                                                                      New PropertyMetadata(False))


Public Shared ReadOnly isDragOverProperty As DependencyProperty = isDragOverPropertyKey.DependencyProperty

Private _isDragOver As Boolean = True
Public Property isDragOver() As Boolean
    Get
        Return CBool(GetValue(isDragOverProperty))
    End Get
    Set(ByVal value As Boolean)
        SetValue(isDragOverProperty, value)
    End Set
End Property

Private Sub Grid_DragEnter(sender As Object, e As Windows.DragEventArgs)
    isDragOver = True
End Sub


Private Sub Grid_DragLeave(sender As Object, e As Windows.DragEventArgs)
    isDragOver = False
End Sub

XAML

 <Grid Margin="0,0,216,63" DragEnter="Grid_DragEnter" DragLeave="Grid_DragLeave" MouseEnter="Grid_MouseEnter_1" MouseLeave="Grid_MouseLeave_1" AllowDrop="True" >
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.Style>
        <Style TargetType="Grid">
            <Style.Triggers>
                <Trigger Property="Elements:ucPageControl.isDragOver" Value="False">
                    <Setter Property="Background" Value="White"></Setter>
                </Trigger>
                <Trigger Property="Elements:ucPageControl.isDragOver" Value="True">
                    <Setter Property="Background" Value="Black"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Style>
</Grid>

不知何故,我的网格在我的财产发生变化时没有意识到。有人在这帮忙吗?

1 个答案:

答案 0 :(得分:0)

创建一个继承Grid并包含dependancy属性的新类。并在xaml中使用它。

<controls:MyGrid Margin="0,0,216,63" DragEnter="Grid_DragEnter" DragLeave="Grid_DragLeave" MouseEnter="Grid_MouseEnter_1" MouseLeave="Grid_MouseLeave_1" AllowDrop="True" >
    <controls:MyGrid.RowDefinitions>
        <RowDefinition></RowDefinition>
    </controls:MyGrid.RowDefinitions>
    <controls:MyGrid.Style>
        <Style TargetType="controls:MyGrid">
            <Style.Triggers>
                <Trigger Property="IsDragOver" Value="False">
                    <Setter Property="Background" Value="White"></Setter>
                </Trigger>
                <Trigger Property="IsDragOver" Value="True">
                    <Setter Property="Background" Value="Black"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </controls:MyGrid.Style> 
</controls:MyGrid>