【发布时间】:2012-12-27 11:31:02
【问题描述】:
我已经设置了我自己的用户控件,它包含两个 ComboBoxes,每个 ComboBox 项目源都绑定到一个 DependencyProperty。我遇到的问题是将使用我的用户控件的表单中的属性传递给用户控件。
下面是我的用户控件:
public static readonly DependencyProperty ListOneProperty = DependencyProperty.Register
("ListOne", typeof(List<int>), typeof(LinkedComboBox), new PropertyMetadata(new List<int>()));
public List<int> ListOne
{
get { return (List<int>)GetValue(ListOneProperty); }
set { SetValue(ListOneProperty, value); }
}
public static readonly DependencyProperty ListTwoProperty = DependencyProperty.Register
("ListTwo", typeof(List<string>), typeof(LinkedComboBox), new PropertyMetadata(new List<string>()));
public List<string> ListTwo
{
get { return (List<string>)GetValue(ListTwoProperty); }
set { SetValue(ListTwoProperty, value); }
}
public LinkedComboBox()
{
InitializeComponent();
FillListOne();
}
下面是我的 MainWindow xaml:
<control:LinkedComboBox x:Name="LinkedBox" ListTwo="{Binding MyList}"/>
还有 MainWindow.xaml.cs:
static List<string> _myList = new List<string>{"abc","efg"};
public List<string> MyList
{
get { return _myList; }
set { _myList = value; }
}
public MainWindow()
{
InitializeComponent();
}
我需要什么才能让用户控件接受来自主窗口的绑定?
【问题讨论】:
-
你不应该在主视图中那样做,但你需要使用 INotifyPropertyChanged stackoverflow.com/questions/1315621/…
-
为什么我不应该从 MainView 执行此操作?如果我拥有的列表是在主窗体中创建的,我应该如何将它放到这个控件中?我一般是 WPF 新手,所以任何提示都将不胜感激。
-
如果您刚刚开始,在获得一些知识后,您可能想学习像 MVVM(或 MVP 或 MVC)这样的模式,以更好地掌握事情的真正工作原理,而不仅仅是放置所有数据在主视图中绑定它,google mvvm,你可以得到很多例子
标签: wpf binding parameter-passing dependency-properties