2015-06-25 41 views
0

我有一个main.xaml文件。在main.xaml文件中,它指向另一个xaml文件中的列表框。这是使用视图调用:LayoutViewListwpf:绑定到另一个xaml文件中的控件

在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') 
+1

为什么你不使用相同的列表中的两个xaml?我不确定你可以引用另一个xaml文件... – Kram

+0

因为列表框控件有很多代码,所以它已被移动到另一个xaml文件。我如何从main.xaml访问列表框? – user3753452

+0

好的。所以你需要创建一个可以获取数据的UserControl。然后,您可以重新使用您的客户usercontrol无论你想要 – Kram

回答

2

您需要使用RelativeSource Binding如果与Binding的观点是你MainView文件的一个孩子。试试这个:

<Button IsEnabled="{Binding DataContext.SelectedItems.Count, RelativeSource={ 
    RelativeSource AncestorType={x:Type YourPrefix:MainView}}" /> 

Binding是指在被设置为的UserControlWindow命名MainViewDataContext对象由SelectedItems财产曝光的物体的Count财产。

+0

非常感谢。请详细说明这一点:绑定DataContext.SelectedItems.Count – user3753452

+0

请看我更新的答案。 – Sheridan