C# 如何在UserControl中设置ComboBox ItemsSource的绑定?
以下是我的UserControl中的组合框:C# 如何在UserControl中设置ComboBox ItemsSource的绑定?,c#,wpf,binding,C#,Wpf,Binding,以下是我的UserControl中的组合框: <Combobox ItemsSource="{Binding ComboItemsProperty}" /> 然而,这不起作用。我认为我做的bind.Source是错误的,但我不确定该将Source设置为什么。此代码位于my UserControl.xaml.cs内。您可以尝试(替换此代码) Binding bind=新绑定(); bind.Mode=BindingMode.OneWay; bind.Source=yourCollec
<Combobox ItemsSource="{Binding ComboItemsProperty}" />
然而,这不起作用。我认为我做的bind.Source是错误的,但我不确定该将Source设置为什么。此代码位于my UserControl.xaml.cs内。您可以尝试(替换此代码)
Binding bind=新绑定();
bind.Mode=BindingMode.OneWay;
bind.Source=yourCollectionSourceOrClass// 您需要设置包含属性的实例的上下文ComboItemsProperty。因此,您应该将其设置为“this.DataContext”或包含您定义的ItemSource属性的其他类对象实例,而不是“this”
试试这个
Binding bind = new Binding();
bind.Mode = BindingMode.OneWay;
bind.Source = this.DataContext;
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBinding( ComboBox. ItemsSource Property, bind);
(发布frm mobile)我已经尝试了很多方法来实现这一点,但似乎没有任何效果
而是在保存.xaml文件时序列化绑定。这似乎是完美的工作。不再需要在代码中设置绑定。为什么要同时在XAML和代码隐藏中设置绑定?可能会重复?请确保不在本地更改ItemsSource的值,这将使绑定无效。除此之外,您的代码似乎还不错。查看VisualStudio中的输出窗口,它给您带来了什么绑定错误?@B-Rad我的帮助了吗?:D@MarkKall你能回答我关于这个链接的问题吗?我也有类似的未解决的问题。你能回答我关于这个链接的问题吗?我也有类似的未解决的问题。
Binding bind = new Binding();
bind.Mode = BindingMode.OneWay;
bind.Source = yourCollectionSourceOrClass; //<--Replace with your collection
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBinding(ComboBox.ItemsSourceProperty, bind);
Binding bind = new Binding();
bind.Mode = BindingMode.OneWay;
bind.Source = this.DataContext;
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBinding( ComboBox. ItemsSource Property, bind);