2017-03-07 70 views
1

爲什麼這是工作:WPF =>綁定文本菜單與CollectionViewSource

<Button x:Name="btnCritereAdd" Content="{Binding Source={x:Static resx:resMain.lblCriterAdd}}" Style="{StaticResource btnStandardClr}" Click="btnMenuPopup_Click" ContextMenuService.Placement="Bottom"> 
    <Button.ContextMenu> 
     <ContextMenu x:Name="cmuCriteres"> 
      <ContextMenu.ItemsSource> 
       <Binding Path="CriteresDispo" /> 
      </ContextMenu.ItemsSource> 
      <ContextMenu.InputBindings> 
       <MouseBinding MouseAction="LeftClick" Command="" /> 
      </ContextMenu.InputBindings> 
     </ContextMenu> 
    </Button.ContextMenu> 
</Button> 

但不是這樣的:

btnCritereAdd.DataContext = vmFiltresChamps; 

我:

<Button x:Name="btnCritereAdd" Content="{Binding Source={x:Static resx:resMain.lblCriterAdd}}" Style="{StaticResource btnStandardClr}" Click="btnMenuPopup_Click" ContextMenuService.Placement="Bottom"> 
    <Button.ContextMenu> 
     <ContextMenu x:Name="cmuCriteres"> 
      <ContextMenu.Resources> 
       <CollectionViewSource x:Key="cvsCriteres" Source="{Binding CriteresDispo}"/> 
      </ContextMenu.Resources> 
      <ContextMenu.ItemsSource> 
       <Binding Source="{StaticResource cvsCriteres}" /> 
      </ContextMenu.ItemsSource> 
      <ContextMenu.InputBindings> 
       <MouseBinding MouseAction="LeftClick" Command="" /> 
      </ContextMenu.InputBindings> 
     </ContextMenu> 
    </Button.ContextMenu> 
</Button> 

我在代碼隱藏設置的DataContext上的按鈕在兩種情況下嘗試使用「UpdateSourceTrigger = PropertyChanged」和「NotifyOnSourceUpdated = True」,但沒有任何更改。 該清單是空的...

你有什麼想法嗎?

VM方: 物業:

public ItemCollection CriteresDispo { get { return _CriteresDispo; } set { _CriteresDispo = value; RaisePropertyChanged(nameof(CriteresDispo)); } } 

命令通過代碼調用背後

public RelayCommand<ItemCollection> LoadCriteresCommand { get; set; } 

private void LoadCriteres(ItemCollection obj) { 
     var ht = new tblFiltreChamps(); 
     Classes.clsUtils.GetFiltresSel(obj, ht); 
     CriteresDispo = new ItemsControl().Items; 
     if (ht.items.Count > 0) { 
      foreach (var item in ht.items.OrderBy((x) => x.Desc).ToList()) { 
       var mi = new MenuItem() { Header = item.Desc, Tag = item }; 
       mi.Command = AddCritereCommand; 
       mi.CommandParameter = item; 
       CriteresDispo.Add(mi); 
      } 
     } 
     if (CriteresAddAction != null) CriteresAddAction(); 
} 
+0

嘗試將CollectionViewSource移動到按鈕資源,查找資源查找可視化樹和水平查找可能會變得奇特,失敗的情況是檢查輸出窗口是否有綁定錯誤 – MikeT

+0

您好MikeT,我已經嘗試tu把它放在button.resource中,但沒有任何變化。 什麼是水平查找? – david

+0

水平查找正在尋找在相同級別的可視化樹中定義的資源 – MikeT

回答

0

CollectionViewSource沒有實現IEnumerable,因此不能被設置爲ContextMenuItemsSource屬性,它是類型IEnumerable

因此,你需要改變:

<ContextMenu.ItemsSource> 
    <Binding Source="{StaticResource cvsCriteres}"/> 
</ContextMenu.ItemsSource> 

要:

<ContextMenu.ItemsSource> 
    <Binding Source="{StaticResource cvsCriteres}" Path="View"/> 
</ContextMenu.ItemsSource> 

這將設置ItemsSourceCollectionViewSourceView屬性,這是CollectionView型和實現IEnumerable

0

Do you have an idea ?

當綁定到StaticResource時,當您爲與CollectonViewSourceSource屬性綁定的屬性引發PropertyChanged事件時,目標屬性不會得到更新。

這就是區別。

您需要綁定到財產,提高PropertyChanged事件這個特定的屬性爲ItemsSource收集得到刷新。