WPF ComboBox 绑定 ItemsSource [英] WPF ComboBox binding ItemsSource

查看:46
本文介绍了WPF ComboBox 绑定 ItemsSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WPF 的初学者,正在尝试将 ComboBox 的项目绑定到 ObservableCollection

I'm a beginner on WPF and trying to bind the Items of a ComboBox to an ObservableCollection

我使用了这个代码:

XAML

<Window x:Class="comboBinding2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="cmbTest" ItemsSource="{Binding Path=cmbContent}" Width="200" VerticalAlignment="Center" HorizontalAlignment="Center" />
    </Grid>
</Window>

C#

public MainWindow()
{
    cmbTest.ItemsSource = cmbContent;
    cmbContent.Add("test 1");
    cmbContent.Add("test 2");

    InitializeComponent();
}

public ObservableCollection<string> cmbContent { get; set; }

在我尝试调试之前,我没有收到有关此代码的任何错误,它会引发错误:

I don't get any errors on this Code until I try to debug, it throws the error:

目标调用错误

PresentationFramework.dll 中发生类型为System.Reflection.TargetInvocationException"的未处理异常

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll

谁能告诉我我做错了什么?

Can anybody tell me what I'm doing wrong?

推荐答案

您当前的实施存在一些问题.正如其他人所说,您的列表当前为 NULL,并且未设置 Window 的 DataContext.

There are a few things wrong with your current implementation. As others have stated, your list is currently NULL, and the DataContext of the Window is not set.

不过,我建议(尤其是因为您刚开始使用 WPF)正在学习使用 MVVM 以更正确"的方式进行绑定.

Though, I would recommend (especially since you just started using WPF) is learning to do the binding the more 'correct' way, using MVVM.

参见下面的简化示例:

首先,您要设置WindowDataContext.这将允许 XAML '查看' ViewModel 中的属性.

First, you want to set the DataContext of your Window. This will allow the XAML to 'see' the properties within your ViewModel.

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

接下来,只需设置一个 ViewModel 类,该类将包含所有 Window 的 绑定元素,例如:

Next, simply set up a ViewModel class that will contain all of the Window's binding elements, such as:

public class ViewModel
{
    public ObservableCollection<string> CmbContent { get; private set; }

    public ViewModel()
    {
        CmbContent = new ObservableCollection<string>
        {
            "test 1", 
            "test 2"
        };
    }
}

最后,更新您的 XAML 以便绑定路径与集合匹配:

Lastly, update your XAML so that the binding path matches the collection:

<Grid>
    <ComboBox Width="200"
          VerticalAlignment="Center"
          HorizontalAlignment="Center"
          x:Name="cmbTest"
          ItemsSource="{Binding CmbContent}" />
</Grid>

这篇关于WPF ComboBox 绑定 ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆