[英]wpf : Bind to a control in another xaml file
我有一个main.xaml文件。 在main.xaml文件中,它引用另一个xaml文件中的列表框。 使用view:LayoutViewList进行调用
在main.xaml文件中,有一个按钮。 仅当选择列表框时,该按钮才会启用。 看起来ElementName = view.LayoutViewList.LayoutListBox无法正常工作。 非常感谢你
Button IsEnabled="{Binding ElementName=view:LayoutViewList.LayoutListBox, Path=SelectedItems.Count}"
绑定错误:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=view:LayoutViewList.LayoutListBox'. BindingExpression:Path=SelectedItems.Count; DataItem=null; target element is 'Button' (Name=''); target property is 'IsEnabled' (type 'Boolean')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=view:LayoutViewList.LayoutListBox'. BindingExpression:Path=SelectedIndex; DataItem=null; target element is 'Button' (Name=''); target property is 'NoTarget' (type 'Object')
你需要使用RelativeSource Binding
如果与视图Binding
是你的孩子MainView
文件。 尝试这个:
<Button IsEnabled="{Binding DataContext.SelectedItems.Count, RelativeSource={
RelativeSource AncestorType={x:Type YourPrefix:MainView}}" />
此Binding
引用由对象中的SelectedItems
属性公开的对象的Count
属性,该对象设置为名为MainView
的UserControl
或Window
的DataContext
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.