2012-10-31 16 views
0

所以我有一個TreeView,它具有在最底層有複選框的層次數據模板。這些CheckBox綁定到ViewModel中的「isChecked」布爾值。我一直試圖做的是加載各種變量,向我指示要檢查哪些複選框,將模型中的布爾值IsChecked更改爲true,從而導致TreeView中的特定CheckBox被更新。這裏是我的參考代碼:如何可視地更新UI中綁定到WPF c#中的boolean isChecked變量的複選框?

XAML:在視圖模型(樹視圖我有工作)

<DockPanel Name="test1" Margin="10,10,0,10" VerticalAlignment="Stretch" Grid.Row="3" Grid.RowSpan="7" Grid.Column="0"> 
     <DockPanel.Resources> 
      <local:CheckBoxCommand x:Key="cbc"></local:CheckBoxCommand> 
      <src:TreeViewFilter x:Key="MyList" /> 

      <HierarchicalDataTemplate DataType="{x:Type src:TreeViewParent}" ItemsSource="{Binding Path=OrderAttributes}"> 
       <TextBlock Text="{Binding Path=NameAndCount}" FontSize="24"/> 
      </HierarchicalDataTemplate> 

      <HierarchicalDataTemplate DataType="{x:Type src:OrderAttribute}" ItemsSource="{Binding Path=OrderAttributes}"> 
       <StackPanel Name="test" Orientation="Horizontal" VerticalAlignment="Center"> 
        <CheckBox Command="{StaticResource cbc}" 
           CommandParameter="{Binding Path=NameAndParent}" Visibility="{Binding Path=CheckBoxVisible}" IsChecked="{Binding Path=isChecked, Mode=TwoWay}" VerticalAlignment="Center"> 
        </CheckBox> 
        <TextBlock Text="{Binding Path=NameAndCount}" FontSize="16"/> 
        </StackPanel> 
      </HierarchicalDataTemplate> 

     </DockPanel.Resources> 
     <TreeView Name="treeView1" BorderThickness="2" ItemsSource="{Binding Source={StaticResource MyList}, UpdateSourceTrigger=PropertyChanged}" TreeViewItem.Selected="filterByBatchStatus"/> 
    </DockPanel> 

C#代碼(關於這個問題):

public event PropertyChangedEventHandler PropertyChanged2; 

protected void FirePropertyChanged2(string CheckBoxChecked) 
    { 
     if (PropertyChanged2 != null) 
     { 
      PropertyChanged2(this, new PropertyChangedEventArgs(CheckBoxChecked)); 
     } 
    } 

public static bool cbc; 

public bool CheckBoxChecked 
    { 
     get { return (bool)GetValue(CheckBoxCheckedProperty); } 
     set { SetValue(CheckBoxCheckedProperty, value); } 
    } 

public static readonly DependencyProperty CheckBoxCheckedProperty = 
     DependencyProperty.Register("CheckBoxChecked", typeof(bool), typeof(OrderAttribute), new UIPropertyMetadata((bool)false)); 

bool _isChecked; 
    public bool isChecked 
    { 
     get { return _isChecked; } 

     set 
     { 
      if (_isChecked != value) 
      { 
       _isChecked = value; 
       FirePropertyChanged2("CheckBoxChecked"); 
      } 
     } 
    } 

如果我加載值,更新布爾值,然後展開樹(所以我可以直觀地看到它們),檢查正確的複選框,一切正常。當最初直觀地看到第一個加載的複選框後,加載不同的複選框,改變布爾值,然後複選框不會根據改變(根本不改變)。

使我困惑的部分是,如果我做了多個負載,一遍又一遍地更改布爾值,不展開樹並且可視化地看到這些框,然後展開樹,檢查正確的框。只要我可以直觀地展開並查看複選框,則無論是否展開或取消展開樹,任何後期加載都不會更新複選框。

+0

它看起來像你'只對「CheckBoxChecked」執行「FirePropertyChanged2」,而不是「isChecked」,你將綁定 – Pyritie

回答

2

哇......這裏有一些嚴重的問題。首先,正如我在你之前的問題中所說的那樣,DependencyProperties don't belong into ViewModels。其次,CheckBox位於爲Type src:OrderAttribute定義的HierarchicalDataTemplate中,因此所有綁定都將搜索該類型的屬性。第三,我不清楚你用什麼字符串參數調用PropertyChanged事件,但它應該是你的類中的CLR屬性的名稱,UI綁定到該名稱。我強烈建議您在嘗試執行復雜的分層表示層之前,至少了解一下This WPF Tutorial,以便至少了解MVVM,INotifyPropertyChanged和DataTemplating的基礎知識。

編輯:從OrderAttribute類別中刪除的DependencyProperty,並調用的PropertyChanged當使用普通布爾屬性,然後,使含有該屬性的名稱的字符串參數,這樣做的:

public bool MyBool 
{ 
    get { return _mybool; } 
    set { 
     _mybool = value; 
     NotifyPropertyChanged("MyBool"); 
     } 
} 

public void NotifyPropertyChanged(string propertyName) 
{ 
    if (PropertyChanged != null) 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName); 
} 
+0

對於你的第二點,布爾值在OrderAttribute類中。如果您查看複選框旁邊的文本塊,我嘗試模仿相同類型的綁定,因爲它用於對OrderAttribute(這是我的模型,可能是令人困惑的模型/視圖模型)中的數據進行文本塊綁定。對於第三點,你是對的,它不是一個字符串。我試圖使用布爾,但PropertyChanged()事件處理程序只有一個字符串參數。至於第一點,我不知道,我用它在樹中的TextBlock,它工作正常,並立即更新UI –

+0

如果您在應用程序的非可視元素中定義DependencyProperties,將遇到嚴重問題(如Models或ViewModels)。 DependencyObjects具有線程關聯性,這意味着您不能從不同於創建線程的線程修改它們的任何屬性,如果您打算將它們綁定到UI,則它們必須是UI線程。 –

+0

我試過你的編輯,沒有依賴屬性,還是一樣的錯誤。我的例子中的propertyName是什麼?只是不明白爲什麼當我將isChecked從false更改爲true時,然後展開它的工作原理。但隨後的任何後續更改不會在視覺上更新,樹擴展或不 –

相關問題