2013-09-27 66 views
0

我有一個綁定一個TreeView到具有「器isChecked」,即與在樹視圖複選框結合使用屬性的類。我需要做的是每當複選框被選中/取消選中相關的「IsChecked」屬性被調整。數據綁定複選框「器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之後,我在上面的「Set」中放置了RaiseEvent PropertyChanged(Me,New PropertyChangedEventArgs(「IsChecked」))。建議here

  • 我創建了「檢查」和「未選中」複選框處理器的屬性。這個問題是他們會在所有其他代碼之後運行。建議here

更新:

  • 我現在已經失敗使用OnPropertyChanged(新PropertyChangedEventArgs( 「器isChecked」))試過之後isCheckedLocal =值

    <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 
    
+1

你的類需要實現INotifyPropertyChanged的,你必須提高對器isChecked屬性的屬性更改事件, 你在二傳手做到這一點你想要通知你的財產的財產。 –

+0

我的班已經這樣做了,但是我沒有證明這一點。 – user

回答

0

爲了使INotifyPropertyChange工作,您必須提高屬性更改。這是C#,但您明白:

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

注意到字符串與屬性名稱完全相同。然後在幕後進行連線。

+0

我試過了,但沒有奏效。看到我上面的編輯。 – user

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) 
+0

感謝您的建議,但這也沒有奏效。 – user

相關問題