![](/img/trans.png)
[英]How to enable/disable comboBox items for different comboBoxes in wpf by click in one using MVVM pattern?
[英]How to bind one ObservableCollection as ItemsSource to different comboBoxes (models) if using wpf ItemsControl with MVVM pattern?
我使用MVVM模式使用wpf ItemsControl元素動態創建到不同模型的comboBoxes列表。 我想要一種邏輯,例如,如果我在comboBoxes之一中選擇一個元素,那么在所有其他元素中它都不會出現。 我在使用ItemsControl(ItemsSource-我的模型的列表)並在VievModel中為其創建元素時遇到困難-綁定不起作用,僅當我為每個模型(Model類)有一個comboBox項目列表時,它才起作用適用於所有comboBoxes(在ViewModel類中)。 例如,我可以為ViewModel中的comboBox項提供1個ObservableCollection,還可以使用ItemsControl創建comboBoxes嗎?
我的觀點 :
<ItemsControl ItemsSource="{Binding Items}" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="{Binding Name}" Grid.Column="0" />
<ComboBox ItemsSource="{Binding ComboBoxItems}" Grid.Column="1"
SelectedItem="{Binding SelectedItem}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
ViewModel:
public ObservableCollection<Model> Items { get; set; }
// if I add here public ObservableCollection<string> ComboBoxItems { get; set; }
// binding isn't working, so I add it to Model class, but in it it does not work as I need.
public ViewModel()
{
Items = new ObservableCollection<Model>();
Items.Add(new Model {Name = "11111"});
Items.Add(new Model {Name = "22222"});
}
模型:
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public string SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
public ObservableCollection<string> ComboBoxItems {get;set;}
public Model()
{
ComboBoxItems = new ObservableCollection<string>();
ComboBoxItems.Add("q");
ComboBoxItems.Add("w");
ComboBoxItems.Add("e");
ComboBoxItems.Add("r");
ComboBoxItems.Add("t");
ComboBoxItems.Add("y");
}
1個ViewModel中的comboBox項的ObservableCollection,還可以使用ItemsControl來創建comboBoxes,這意味着您想將相同的ObservableCollection綁定到ur ItemsControl
和ComboBox
,否則請在評論中告知我
安斯
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="{Binding Name}" Grid.Column="0" />
<ComboBox ItemsSource="{Binding Items ,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}" Grid.Column="1"
DisplayMemberPath="Name" SelectedItem="{Binding SelectedItem}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我將Items
綁定到ComboBox
為了實現您的目標,您可以過濾ComboBoxItems
視圖。 您可以以單源單視圖的方式進行操作,只需更改ViewModel
的構造函數,例如:
public ViewModel()
{
Items = new ObservableCollection<Model>();
Items.Add(new Model {Name = "11111"});
Items.Add(new Model {Name = "22222"});
Items.Add(new Model {Name = "33333"});
foreach (var item in Items)
{
CollectionViewSource.GetDefaultView(item.ComboBoxItems).Filter =
(x) => !Items.Where((y) => y != item).Select(y => y.SelectedItem).Any(y => y == (string)x);
item.PropertyChanged += (s, e) =>
{
foreach (var obj in Items.Where((x) => x != item).Select(x => x.ComboBoxItems))
CollectionViewSource.GetDefaultView(obj).Refresh();
};
}
}
或者,您也可以單源多視圖的方式進行操作,從而可以丟棄模型。
public class ViewModel
{
private List<string> _comboBoxItems = new List<string> { "q", "w", "e", "r", "t", "y" };
private List<ICollectionView> _comboBoxViews = new List<ICollectionView>();
public ObservableCollection<string> Names { get; set; } = new ObservableCollection<string> { "111", "222", "333" };
public ICollectionView ComboBoxView
{
get
{
var view = new CollectionViewSource { Source = _comboBoxItems}.View;
_comboBoxViews.Add(view);
view.MoveCurrentToPosition(-1);
view.Filter = (x) => !_comboBoxViews.Where(y => y != view).Any(y => (string)y.CurrentItem == (string)x);
view.CurrentChanged += (s, e) =>
{
foreach (var v in _comboBoxViews.Where(x => x != view))
v.Refresh();
};
return view;
}
}
}
<ItemsControl ItemsSource="{Binding Names}" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="{Binding}" Grid.Column="0" />
<ComboBox ItemsSource="{Binding DataContext.ComboBoxView, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
Grid.Column="1"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.