Wpf DataTrigger没有触发属性更改

时间:2017-09-27 12:51:06

标签: wpf vb.net xaml

我正在尝试设置一个列表视图,根据绑定显示所选项目为蓝色,但数据触发器不会触发。如果我将触发值设置为0(初始值),则listviewitems将以蓝色背景创建,但之后不会更改。

VB代码:

Private Sub SetGridViewDynamically()
    Dim myGridView As New GridView
    myGridView.AllowsColumnReorder = True

    Dim gvc1 As New GridViewColumn
    gvc1.DisplayMemberBinding = New Binding("A")
    gvc1.Header = "1"
    gvc1.Width = TestListView.ActualWidth * 0.19
    myGridView.Columns.Add(gvc1)

    Dim gvc2 As New GridViewColumn
    gvc2.DisplayMemberBinding = New Binding("B")
    gvc2.Header = "2"
    gvc2.Width = TestListView.ActualWidth * 0.39
    myGridView.Columns.Add(gvc2)

    Dim gvc3 As New GridViewColumn()
    gvc3.DisplayMemberBinding = New Binding("C")
    gvc3.Header = "3"
    gvc3.Width = TestListView.ActualWidth * 0.19
    myGridView.Columns.Add(gvc3)

    Dim gvc4 As New GridViewColumn()
    gvc4.DisplayMemberBinding = New Binding("D")
    gvc4.Header = "4"
    gvc4.Width = TestListView.ActualWidth * 0.19
    myGridView.Columns.Add(gvc4)

    TestListView.View = myGridView

    Dim style As New Style
    style.TargetType = GetType(ListViewItem)
    style.Setters.Add(New Setter(ListViewItem.HorizontalContentAlignmentProperty, HorizontalAlignment.Center))
    Dim x = New DataTrigger
    x.Binding = New Binding("rowselected")
    x.Value = 0
    x.Setters.Add(New Setter(TestListView.BackgroundProperty, DarkGradientSample.Background))

    Dim x2 = New Trigger
    x2.Property = ItemsControl.AlternationIndexProperty
    x2.Value = 1
    x2.Setters.Add(New Setter(TestListView.BackgroundProperty, LightGradientSample.Background))

    Dim x3 As New DataTrigger
    x3.Binding = New Binding("rowselected")
    x3.Value = 1
    x3.Setters.Add(New Setter(TestListView.BackgroundProperty, BlueGradientSample.Background))

    style.Triggers.Add(x)
    style.Triggers.Add(x2)
    style.Triggers.Add(x3)

    TestListView.ItemContainerStyle = style

 End Sub

和列表视图的XAML:

<ListView x:Name="TestListView" Grid.Column="1" Margin="10" Grid.Row="2" ItemsSource="{Binding picks}" FontSize="48" AlternationCount="2" Foreground="White" HorizontalContentAlignment="Stretch">

    </ListView>

编辑:知道了。我没有正确实现Inotifypropertychanged。这是在类

中正确实现的接口
 Public Class PickLocation
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private _rowselected As Integer
    Public Property rowselected As Integer
        Get
            Return _rowselected
        End Get
        Set(value As Integer)
            _rowselected = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("rowselected"))
        End Set
    End Property

1 个答案:

答案 0 :(得分:1)

只要将属性设置为新值,您的类就应该实现INotifyPropertyChanged并引发PropertyChanged事件:

Public Class PickLocation
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(<CallerMemberName()> Optional ByVal propertyName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub


    Private _rowselected As Integer
    Public Property rowselected() As Integer
        Get
            Return _rowselected
        End Get

        Set(ByVal value As Integer)
            If Not(value = _rowselected) Then
              _rowselected = value
                NotifyPropertyChanged()
            End If
        End Set
    End Property
End Class