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" />