0

我将 TreeView 绑定到具有“IsChecked”属性的类,该属性与树视图中的复选框一起使用。我需要发生的事情是,每当检查复选框/未选中复选框时,都会调整相关的“检查”属性。

现在,当我在树视图中取消选中一个复选框时,代码将该属性设置为 false,但随着代码继续并稍后再次引用该属性,该属性再次设置为“True”。谁能解释为什么该物业的新价值不会改变,以及解决方案是什么?

我(失败的)尝试的方法在底部。

这是这样的 XAML:

<TreeView.ItemTemplate>
  <HierarchicalDataTemplate ItemsSource="{Binding Path=BusGrps}">
    <CheckBox Name="ChkBxEnterprise" Content="{Binding Path=Name}" IsEnabled="{Binding Path=Enabled}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"/>
    <HierarchicalDataTemplate.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Programs}">
            <CheckBox Name="ChkBxBusGrp" Content="{Binding Path=Name}" IsEnabled="{Binding Path=Enabled}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"/>
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <CheckBox Name="ChkBxProg" Content="{Binding Path=Name}" IsEnabled="{Binding Path=Enabled}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"/>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
  </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

这是课程:

<Serializable()> Public Class LvlProgram
Implements INotifyPropertyChanged

Public Shared Event selectionChanged(ByVal busgrp As String, ByVal name As String, ByVal selected As System.Nullable(Of Boolean))

Private busGrpLocal As String
Private nameLocal As String
'Private camsLocal As New List(Of LvlCAM)
'Private periodsLocal As New List(Of PeriodScore)
Private camsLocal As New ObservableCollection(Of LvlCAM)
Private periodsLocal As New ObservableCollection(Of PeriodScore)
Private isCheckedLocal As Boolean = True
Private enabledLocal As Nullable(Of Boolean) = False

Public Property Enabled As Nullable(Of Boolean)
    Get
        Return enabledLocal
    End Get
    Set(value As Nullable(Of Boolean))
        enabledLocal = value
    End Set
End Property

Public Property IsChecked As Boolean
    Get
        Return isCheckedLocal
    End Get
    Set(value As Boolean)
        If isCheckedLocal <> value Then
            isCheckedLocal = value
            OnPropertyChanged(New PropertyChangedEventArgs("IsChecked"))
            RaiseEvent selectionChanged(BusGrp, Name, value)
        End If
    End Set
End Property
...

我尝试了以下方法,但都没有奏效:

  • 在更改 isCheckedLocal 后,我将 RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IsChecked")) 放在上面的“Set”中。建议在这里

  • 我为复选框“Checked”和“Unchecked”属性创建了处理程序。问题是它们会在我的所有其他代码之后运行。建议在这里

更新:

  • 我现在尝试在 isCheckedLocal = value 之后使用 OnPropertyChanged(New PropertyChangedEventArgs("IsChecked")) 失败

    <NonSerialized()> Public Event PropertyChanged As PropertyChangedEventHandler     Implements INotifyPropertyChanged.PropertyChanged
    
     Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
     If Not PropertyChangedEvent Is Nothing Then
     RaiseEvent PropertyChanged(Me, e)
     End If
     End Sub
    
4

2 回答 2

0

为了使 INotifyPropertyChange 工作,您必须引发属性更改。这是 C#,但你明白了:

        public DateTime MigrationDate
        {
            get { return _migrationDate; }
            set
            {
                _migrationDate = value;
                RaisePropertyChanged("MigrationDate");
            }
        }

注意字符串与属性名称完全相同。然后这是在幕后连接起来的。

于 2013-09-27T18:55:31.870 回答
0

尝试使用

Public Shared Event selectionChanged(ByVal busgrp As String, ByVal name As String, ByRef selected As System.Nullable(Of Boolean))

代替

Public Shared Event selectionChanged(ByVal busgrp As String, ByVal name As String, ByVal selected As System.Nullable(Of Boolean)
于 2013-09-27T18:58:14.083 回答