2009-09-05 41 views
1

我有一個DataTemplate在StackPanel中顯示按鈕。每當用戶點擊一個按鈕,該按鈕應該發光。所以我在模板中寫了必要的DataTrigger,並且在我綁定的屬性中也有一個布爾條件。這裏是下面的詳細信息:WPF DataTemplate和屬性中的奇怪行爲。

<DataTemplate x:Key="ActionItemsTemplate" DataType="ActionItemViewModel"> 

     <ItemsControl IsTabStop="False" ItemsSource="{Binding}" Margin="6,2"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate>      
        <StackPanel Orientation="Horizontal" />       
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 

      <ItemsControl.ItemTemplate> 
       <DataTemplate> 

        <Button x:Name="ActionButton" Command="{Binding Path=Command }" Content="{Binding Path=DisplayName}" Style="{DynamicResource HeaderButton}"/> 
        <!-- Set special values for Selected Item --> 
        <DataTemplate.Triggers> 
         <DataTrigger Binding="{Binding IsSelected}" Value="True"> 
          <Setter TargetName="ActionButton" Property="Style" Value="{DynamicResource MainWindowSelectedButton}"/> 
          <!--Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}"--> 
         </DataTrigger> 
        </DataTemplate.Triggers> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </DataTemplate> 

我已經實現INotifyPropertyChanged接口和物業ActionItems返回一個ObservableCollection。

問題:當我更改ObservableCollection並調用INotifyPropertyChanged事件時,它不直接反映在DataTemplate中,而只是改變屬性。但是,如果我重新確定整個對象與它自己,它完美的作品。

e.g

void Test1() 
     { 
      _commands[0].IsSelected = !_commands[0].IsSelected;    
      _commands[0] = _commands[0];  // Does not work If this line is commented out   
      ActionItems = _commands; 
     } 

還有什麼問題呢?

編輯:我發現問題可能是在這種情況下的DataBinding。

我已經到達類似的問題,現在我已經將Expander控件的IsExpanded屬性綁定到TabPanel中的bool屬性。當我切換bool屬性時,該值將在後面更改,但不會反映在顯示中。不過,假設我改變標籤並返回,我發現已經發生了變化。這是與這個問題here

我又知道什麼問題可以(把範圍縮小一點:))

編輯2方案:針對第二個問題:我發現INotifyPropertyChanged接口的OnPropertyChangedEvent需要被調用時關於 IsExpanded屬性的程序更新已更新。至於原始問題,這似乎並不是這樣,我仍然試圖找出哪裏出了問題。 :)

回答

0

我被引導認爲你不能取代集合,只能改變它的內容。

+0

是的,在改變其內容,然後引發INotifyPropertyChanged事件處理程序,該屬性的get方法被調用,並且期望的值被返回,但我無法弄清楚,爲什麼我需要重新分配變量再來一次........... – 81967 2009-09-05 13:29:43

0

該財產的布爾條件,我也結合

我不完全知道你的意思在這裏,但我走的猜測,這是你的「命令」 IsSelected屬性而且你忘記爲該屬性提出PropertyChanged。