爲什麼這是工作: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();
}
嘗試將CollectionViewSource移動到按鈕資源,查找資源查找可視化樹和水平查找可能會變得奇特,失敗的情況是檢查輸出窗口是否有綁定錯誤 – MikeT
您好MikeT,我已經嘗試tu把它放在button.resource中,但沒有任何變化。 什麼是水平查找? – david
水平查找正在尋找在相同級別的可視化樹中定義的資源 – MikeT