C# 为什么这个组合框数据绑定不起作用?

C# 为什么这个组合框数据绑定不起作用?,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,我有一个小的测试窗口,如下所示: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="5

我有一个小的测试窗口,如下所示:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="Combo" DisplayMemberPath="Word" ItemsSource="{Binding Numbers}" HorizontalAlignment="Left" Margin="115,27,0,0" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>
ItemsSource="{Binding Numbers,
    RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Numbers = new ObservableCollection<NumberWord>
                      {
                          new NumberWord {Number = 1, Word = "One"},
                          new NumberWord {Number = 2, Word = "Two"},
                          new NumberWord {Number = 3, Word = "Three"}
                      };
        Combo.ItemsSource = Numbers;
    }
    public ObservableCollection<NumberWord> Numbers { get; set; }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
数字=新的可观察集合
{
新的数字词{Number=1,Word=“One”},
新的数字词{Number=2,Word=“Two”},
新数字词{Number=3,Word=“Three”}
};
Combo.ItemsSource=数字;
}
公共可观测集合数{get;set;}
}
我一直看到我的其他绑定问题的答案,其中说明了
Combo.ItemsSource=Numbers的显式设置是不必要的,因为我有绑定
ItemsSource=“{binding Numbers}”
。我还被多次告知,我不需要在
组合上设置
DataContext
,因为整个窗口都是数据上下文,而
Combo
继承了此数据上下文


我的问题是,为什么我总是——不仅仅是这个组合——在代码隐藏中显式地设置
ItemsSource
或其他绑定属性。为什么XAML数据绑定不起作用?

您需要实现INotifyPropertyChanged。然后使用:

this.DataContext = this;

绑定
ItemsSource=“{binding Numbers}”
需要一个源对象,即具有公共
Numbers
属性的对象。在您的例子中,这是MainWindow实例

因此,要么通过如下方式显式设置源代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="Combo" DisplayMemberPath="Word" ItemsSource="{Binding Numbers}" HorizontalAlignment="Left" Margin="115,27,0,0" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>
ItemsSource="{Binding Numbers,
    RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
或者通过设置
数据上下文来设置源对象,例如在代码隐藏中:

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
    ...
}
如果以后需要更改
Numbers
属性的值,还必须添加属性更改通知机制,如实现
INotifyPropertyChanged
接口



MSDN上的文章对此进行了很好的解释。

DataContext
是子控件从父控件继承的
依赖属性之一,如果未设置本地值

因此,如果将
窗口
DataContext
设置为某物,则包括您的
组合框
在内的子控件将拥有它

现在,在您的情况下,您没有设置
窗口的
DataContext
,因此
ItemsSource的
绑定不起作用

如果在窗口的构造函数中,在初始化Component之后

DataContext = this;

然后您不需要在
code behind
中设置
组合框的
ItemsSource
,而
ItemsSource
的绑定将在您的组合上起作用。

我不需要实现
INotifyPropertyChanged
,因为没有任何更改,而且
数字只不过是数据上下文的一个成员,因此,如果我设置
DataContext=Numbers
,那就太愚蠢了。其他控件将共享此数据上下文,并希望绑定到除
Numbers
@ProfK之外的其他成员:您是对的,也许最好的选择是对多个控件和单个数据上下文使用MVVM。抱歉,我更正了对此的更改答案。DataContext=此;