繁体   English   中英

wpf:绑定到另一个xaml文件中的控件

[英]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属性,该对象设置为名为MainViewUserControlWindow DataContext

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM