【问题标题】:Binding combobox to another combobox将组合框绑定到另一个组合框
【发布时间】:2016-08-23 06:57:06
【问题描述】:

我在将组合框绑定到另一个组合框时遇到问题。我正在尝试将参数(id)从第一个组合框动态传递给启动第二个组合框的方法。例如,如果我在第一个组合框中选择了第一项,那么第二个组合框将使用从第一个组合框中选择的参数进行初始化。

XAML:

<ComboBox Name="ItServiceCmbox" ItemsSource="{Binding ItServiceMetricsNames}" DisplayMemberPath="ServiceName" SelectedValuePath="ServiceId" />
<ComboBox Name="MetricCmbox" ItemsSource="{Binding SelectedItem.MetricId, ElementName=ItServiceCmbox}" DisplayMemberPath="MetricName" SelectedValuePath="MetricId"/>

C#:

public partial class MainWindow : Window
{
    readonly MetricsValuesHelper _metricsValuesHelper = new MetricsValuesHelper(new Repository());
    public static int SelectedService;
    public static int SelectedMetric;
    public ObservableCollection<ItServiceMetricsNames> ItServiceMetricsNames { get; set; }      

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        SelectedService = Convert.ToInt32(ItServiceCmbox.SelectedItem);
        ItServiceMetricsNames = new ObservableCollection<ItServiceMetricsNames>();
        ItServiceMetricsNames.Add(new ItServiceMetricsNames()
        {
            ServiceId = _metricsValuesHelper.GetServiceId(),
            ServiceName = _metricsValuesHelper.GetServiceName(),
            MetricId = _metricsValuesHelper.GetMetricId(SelectedService),
            MetricName = _metricsValuesHelper.GetMetricName(SelectedService)
        });
    }
}

ItServiceMetricsNames 类:

public class ItServiceMetricsNames
{
    public List<int> ServiceId { get; set; }
    public List<string> ServiceName { get; set; }
    public List<int> MetricId { get; set; }
    public List<string> MetricName { get; set; }
}

有可能吗?感谢您的任何回答!

【问题讨论】:

  • 不清楚。第二个 ComboBox 必须显示什么?身份证、姓名……!!
  • 有多种 IT 服务。每个 IT 服务都有一些指标。我必须在第一个组合框中选择 IT 服务,并将其传递给 GetMetricName(SelectedService) 方法,以便在第二个组合框中显示所选(在第一个组合框中)IT 服务的指标。
  • 显示名称,但选择的值必须是Id

标签: c# wpf xaml combobox


【解决方案1】:

这是我去年做的一个混乱、幼稚的实现,但似乎有效。肯定有更好的出路。我没有尝试在我的 xaml 中进行任何实际绑定,而是创建了事件处理程序。您可以为 ComboBox 创建事件处理程序,只要发送的 ComboBox 失去焦点、关闭它的 DropDown、更改选择等,就会触发这些事件处理程序。

如果您希望一个 ComboBox 依赖于另一个,您可以禁用依赖的 ComboBox,直到在独立的 ComboBox 中进行选择。做出选择后,您可以使用适当的数据填充并启用相关的 ComboBox。

您代码中的事件处理程序将如下所示:

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // Independent ComboBox is the sender here
        ProcessComboBoxes(sender as ComboBox);
    }

ProcessComboBoxes 方法看起来会有所不同,具体取决于您要执行的操作。但是,本质上,它将识别您想要有条件地填充的目标/依赖 ComboBox - 使用从 ComboBox 映射到 ComboBox 的 Dictionary 或您认为合适的东西来执行此操作。确定目标后,您将清除之前添加的所有项目,然后重新填充新项目。下面是伪代码中的一个方法(实际)。

    private void ProcessComboBoxes(ComboBox senderBox)
    {
        ComboBox dependentBox = lookupDependent[senderBox];

        var itemType = itemTypes[senderBox.selectedIndex];
        var listOfItemsNeeded = lookupItemsByType[itemType];
        dependentBox.Items.Clear();

        foreach (string item in listOfItemsNeeded){
            dependentBox.Items.Add(item);
        }

        dependentBox.IsEnabled = true;
    }

不要忘记将您的事件处理程序添加到您的 xaml。请务必密切注意事件的调用层次结构,并确定您希望何时重新填充依赖的 ComboBox。

【讨论】:

  • 我想这就是我需要的。感谢您的帮助
猜你喜欢
  • 2012-07-24
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多