gpt4 book ai didi

wpf - 绑定(bind)到相同数据源的互斥组合框 - MVVM 实现

转载 作者:行者123 更新时间:2023-12-03 04:49:24 27 4
gpt4 key购买 nike

我不确定我的标题是否正确,但这就是我现在面临的问题..我有以下 XAML 代码..

        <ItemsControl.ItemTemplate>

<DataTemplate>

<StackPanel Orientation="Horizontal">

<ComboBox ItemsSource="{Binding Path=AvailableFields}"

SelectedItem="{Binding Path=SelectedField}"

></ComboBox>

</StackPanel>

</DataTemplate>

</ItemsControl.ItemTemplate>

这基本上做的是,如果我的数据源包含十个项目,这将生成 10 行组合框,并且所有组合框都绑定(bind)到相同的项目源。

现在我的要求是,一旦在第一个组合框中选择了某个项目,该项目就不应该在后续组合框中可用。在MVVM和WPF中如何满足这个需求?

最佳答案

事实证明,这比我开始编码时想象的要困难。下面的示例可以满足您的要求。组合框将包含仍然可用且未在另一个组合框中选择的所有字母。

XAML:

<Window x:Class="TestApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<StackPanel>
<ItemsControl ItemsSource="{Binding Path=SelectedLetters}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding Path=AvailableLetters}"
SelectedItem="{Binding Path=Letter}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>

</Window>

隐藏代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;

namespace TestApp
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

DataContext = new VM();
}
}

public class VM : INotifyPropertyChanged
{
public VM()
{
SelectedLetters = new List<LetterItem>();
for (int i = 0; i < 10; i++)
{
LetterItem letterItem = new LetterItem();
letterItem.PropertyChanged += OnLetterItemPropertyChanged;
SelectedLetters.Add(letterItem);
}
}

public List<LetterItem> SelectedLetters { get; private set; }

private void OnLetterItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != "Letter")
{
return;
}

foreach (LetterItem letterItem in SelectedLetters)
{
letterItem.RefreshAvailableLetters(SelectedLetters);
}
}

public event PropertyChangedEventHandler PropertyChanged;

public class LetterItem : INotifyPropertyChanged
{
static LetterItem()
{
_allLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Select(c => c.ToString());
}

public LetterItem()
{
AvailableLetters = _allLetters;
}

public void RefreshAvailableLetters(IEnumerable<LetterItem> letterItems)
{
AvailableLetters = _allLetters.Where(c => !letterItems.Any(li => li.Letter == c) || c == Letter);
}

private IEnumerable<string> _availableLetters;
public IEnumerable<string> AvailableLetters
{
get { return _availableLetters; }
private set
{
_availableLetters = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("AvailableLetters"));
}
}
}


private string _letter;
public string Letter
{
get { return _letter; }
set
{
if (_letter == value)
{
return;
}
_letter = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Letter"));
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

private static readonly IEnumerable<string> _allLetters;
}
}
}

关于wpf - 绑定(bind)到相同数据源的互斥组合框 - MVVM 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5174735/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com