Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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_Combobox_Binding - Fatal编程技术网

C# 将字符串列表绑定到组合框的WPF数据

C# 将字符串列表绑定到组合框的WPF数据,c#,wpf,combobox,binding,C#,Wpf,Combobox,Binding,//然后将控件添加到主窗口 当我点击组合框时,它抛出了这个异常 Child child = new Child() my_userControl control = new my_userControl(child) child不应是具有InitializeComponent()方法的局部视图类 它应该是从Base继承的简单派生类: System.Windows.Data Error: 8 : Cannot save value from target back to source. Bind

//然后将控件添加到主窗口

当我点击组合框时,它抛出了这个异常

Child child = new Child()
my_userControl control = new my_userControl(child)

child
不应是具有
InitializeComponent()
方法的局部视图类

它应该是从
Base
继承的简单派生类:

System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=IsDropDownOpen; DataItem='ComboBox' (Name='comboBox'); target element is 'ToggleButton' (Name=''); target property is 'IsChecked' (type 'Nullable`1') XamlParseException:'System.Windows.Markup.XamlParseException: Two-way binding requires Path or XPath. ---> System.InvalidOperationException: Two-way binding requires Path or XPath.
   at System.Windows.Data.BindingExpression.CreateBindingExpression(DependencyObject d, DependencyProperty dp, Binding binding, BindingExpressionBase parent)
然后,您应该设置
UserControl
DataContext
,其中您的XAML被定义为
child
的实例:

public class child : Base
{
    public child()
    {
        PortList.add("com1");
    }
}
当我点击组合框时,它抛出了这个异常

Child child = new Child()
my_userControl control = new my_userControl(child)
将绑定模式更改为单向或绑定到类的属性:

public partial class my_UserControl : UserControl
{
    public my_UserControl()
    {
        InitializeComponent();
        DataContext = new child();
    }
}


组合框是在哪个视图/文件中定义的,您如何设置此视图/文件的DataContext?能否在组合框周围发布更多Xaml?里面嵌套了什么?发布了所有的xaml。如何以及在哪里创建UserControl?另外,您似乎正在将UserControl的DataContext设置为base。按照我在回答中的建议将其设置为child。DataContext将设置为child。显示在我的编辑。我这样做的原因是,我可以有多个孩子绑定到基类中定义的变量。将其更改为单向就成功了!你能解释一下吗?我从来没有设置过模式。错误信息说明了一切;“双向绑定需要路径或XPath”。TextBox文本属性的默认绑定模式为双向。您需要显式地将其设置为单向,以便能够绑定到ObservableCollection中的字符串值。
<TextBox Text="{Binding Path=., Mode=OneWay}" Width="16" Height="16" Margin="0,2,5,2" />