Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定组合框项资源在WPF中不起作用_C#_Wpf_Xaml_Binding_Combobox - Fatal编程技术网

C# 绑定组合框项资源在WPF中不起作用

C# 绑定组合框项资源在WPF中不起作用,c#,wpf,xaml,binding,combobox,C#,Wpf,Xaml,Binding,Combobox,这有点奇怪,因为我在那里找到的每个例子都说我做的事情是正确的,但是我无法让我的ComboBox绑定在WPF中工作 我刚刚创建了一个空的WPF应用程序 public List<string> myCollection { get; set; } public MainWindow() { DataContext = this; InitializeComponent(); myCollection = new List&

这有点奇怪,因为我在那里找到的每个例子都说我做的事情是正确的,但是我无法让我的ComboBox绑定在WPF中工作

我刚刚创建了一个空的WPF应用程序

public List<string> myCollection { get; set; }

    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
        myCollection = new List<string> {"test1", "test2", "test3", "test4"};
    }
public List myCollection{get;set;}
公共主窗口()
{
DataContext=this;
初始化组件();
myCollection=新列表{“test1”、“test2”、“test3”、“test4”};
}
以下是我的xaml:

<Window x:Class="WpfApplication2.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 ItemsSource="{Binding Path=myCollection}" Height="23" HorizontalAlignment="Left" Margin="66,56,0,0" Name="comboBox1" VerticalAlignment="Top" Width="319" />
</Grid>

我尝试过绑定myCollection,Binding Path=myCollection,我尝试过设置DataContext和不设置DataContext。 似乎什么都没用


我的想法已经没有了,我在那里找到的每个示例都表明这是正确的方法,应该可以工作,因此感谢我提供的任何帮助。

InitializeComponent

 InitializeComponent();          
 myCollection = new List<string> { "test1", "test2", "test3", "test4" };
 DataContext = this;
InitializeComponent();
myCollection=新列表{“test1”、“test2”、“test3”、“test4”};
DataContext=this;

在构造函数末尾

comboBox1.ItemsSource = myCollection;

Sajeetheran answer之所以有效,是因为XAML对象的初始化会查看当前状态并绑定到当时的状态,但如果将属性更改为其他属性,则会失败。我会称之为一次性的工作

我只是想使用绑定来实现这一点

对于大多数WPF场景,我们希望使用
INotifyPropertyChange
机制来允许XAML绑定处理动态更改。如果想要真正使用绑定的功能,它需要与INotifyPropertyChange齐头并进。否则,迪米特里的回答和萨吉塔兰的一样有效


绑定不知道更改,因为
myCollection
的引用没有通知世界状态的更改;因此,数据不会显示出来

为方便此类通知,用于保存属性的类需要遵守
INotifyPropertyChanged
,属性
myCollection
需要发送通知事件。(请注意,在您的案例中,它是技术上可行的主窗口,但在MVVM范例中,需要将视图与dat分开,并使用
ViewModel
类保存实际数据并提供此通知)

然后将绑定目标订阅的事件提供给
DataContext
上的项目:

 public event PropertyChangedEventHandler PropertyChanged;
然后是提供更改事件的机制

/// <summary>
/// Raises the PropertyChanged event.
/// </summary>
/// <param name="propertyName">The name of the property that has changed.</param>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
public List myCollection{get;set;}
公共主窗口()
{            
myCollection=新列表{“test1”、“test2”、“test3”、“test4”};
DataContext=this;
InitializeComponent();//--在末尾调用它
}

在分配数据上下文后,您必须初始化组件。

是的,我知道我可以做到,这很有效,我只是想使用bindings@RobertPorter然后您应该按照上面的建议设置DataContext!如果此解决方案不适用于您,请确保已将{get;set;}添加到字段中。
/// <summary>
/// Raises the PropertyChanged event.
/// </summary>
/// <param name="propertyName">The name of the property that has changed.</param>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
private List<string> _myCollection;

public List<string> myCollection
{
    get { return _myCollection; }
    set { _myCollection= value; OnPropertyChanged("myCollection"); }
}
public List<string> myCollection { get; set; }

    public MainWindow()
    {            
        myCollection = new List<string> {"test1", "test2", "test3", "test4"};
        DataContext = this;

        InitializeComponent(); //-- call it at the end
    }