Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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# 组合框项资源绑定不';t使用可观察集合Add更新_C#_Wpf_Xaml - Fatal编程技术网

C# 组合框项资源绑定不';t使用可观察集合Add更新

C# 组合框项资源绑定不';t使用可观察集合Add更新,c#,wpf,xaml,C#,Wpf,Xaml,我有一个简单的wpf应用程序,我想尝试使用来自xaml的ItemsSource绑定。当我点击按钮时。它也应该在UI中更新,但是它没有 为什么不起作用 Xaml代码: <Window x:Class="SendRawEthernetPacketsGUI.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/

我有一个简单的wpf应用程序,我想尝试使用来自xaml的ItemsSource绑定。当我点击按钮时。它也应该在UI中更新,但是它没有

为什么不起作用

Xaml代码:

<Window x:Class="SendRawEthernetPacketsGUI.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">
<Grid>
    <ComboBox HorizontalAlignment="Left" Margin="76,65,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding test}"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="90,171,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

</Grid>

C#代码:

使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
命名空间SendRawEthernetPacketsGUI
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共ObservableCollection测试=新ObservableCollection();
公共窗口1()
{
初始化组件();
DataContext=this;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
添加测试(“FDDF”);
}
}
}

问这个问题感觉有点愚蠢,但即使在谷歌上搜索也帮不了我。你能吗?

你的绑定一开始就不起作用。只能绑定到属性,不能绑定到字段

请尝试以下方法:

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

public Window1()
{
    Test = new ObservableCollection<string>();
}
publicobservableCollection测试{get;set;}
公共窗口1()
{
测试=新的ObservableCollection();
}
或者如果你想要一些复杂的C#6魔法:

publicobservablecollection测试=>newobbservablecollection();
这是一个函数体成员,编译为只读属性,该属性初始化为新的
observateCollection

注意事项/设计错误:


请注意,在这两种情况下,您都没有使用INotifyPropertyChanged,因此用户界面不会获取对集合的批发分配。您还应该为公共属性使用PascalCase,并使用适当的视图模型,而不是绑定到后面的代码。

!谢谢2个问题:为什么只能绑定到属性?字段有什么问题?如果您尝试在代码隐藏中绑定,则可以绑定到字段。2) 什么是C#6中的
=>
?委托?@789这只是规则,与绑定系统如何使用反射来查找绑定目标有关。对于2,=>读取“这样”,通常与lambda表达式一起使用。在本例中,它定义了一个函数体成员。我在我的回答中添加了一些解释。
系统使用反射
explain?@789没有试图解释所有反射(这在C#中是一个相当大的主题),系统会动态地查找具有绑定的
路径
变量中提供的名称的成员。当它这样做时,它只查找属性(而不是字段或方法)。这正是他们选择实施它的方式。这里有一篇关于反思的文章:请注意,这是一个很容易被误用的高级概念。在你熟练掌握.NET编程之前,我会避开它。反射基本上是在运行时创建自定义对象,对吗?
public ObservableCollection<string> test { get; set; }

public Window1()
{
    Test = new ObservableCollection<string>();
}
public ObservableCollection<string> test => new ObservableCollection<string>();