在我的用户控件中,我有这个属性:
public static DependencyProperty FooListProperty = DependencyProperty.Register(
"FooList", typeof(List<Problem>), typeof(ProblemView));
public List<Problem> FooList
{
get
{
return (List<Problem>)GetValue(FooListProperty);
}
set
{
SetValue(FooListProperty, value);
}
}
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == FooListProperty)
{
// Do something
}
}
Run Code Online (Sandbox Code Playgroud)
从另一个窗口开始,我试图为最后一个用户控件设置一个值:
<local:ProblemView HorizontalAlignment="Center"
VerticalAlignment="Center" FooList="{Binding list}" />
Run Code Online (Sandbox Code Playgroud)
负载中的窗口包含:
public List<Problem> list;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Some processes and it sets to list field
list = a;
}
Run Code Online (Sandbox Code Playgroud)
但是在XAML代码中,绑定它是行不通的.不要传递数据.我错了什么?
您无法绑定到 WPF 中的字段,而是必须更改list为属性。
FooList您在XamlUserControl和Xaml 中调用了依赖属性ResultList,但我猜这是问题中的拼写错误。
您应该INotifyPropertyChanged在 中实现Window,让绑定知道该值已更新。
我不确定您DataContext在 Xaml 中是否有正确的设置ProblemView,如果您不确定是否可以命名Window并ElementName在绑定中使用
<Window Name="window"
...>
<!--...-->
<local:ProblemView HorizontalAlignment="Center"
VerticalAlignment="Center"
ResultList="{Binding ElementName=window,
Path=List}" />
<!--...-->
</Window>
Run Code Online (Sandbox Code Playgroud)
后面的示例代码
public partial class MainWindow : Window, INotifyPropertyChanged
{
//...
private List<Problem> m_list;
public List<Problem> List
{
get { return m_list; }
set
{
m_list = value;
OnPropertyChanged("List");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
764 次 |
| 最近记录: |