您需要在第二个绑定上指定ElementName:
<combobox x:Name="cboCustomers" ItemsSource="{Binding Data.Customers}"/>
<combobox x:Name="cboInvoices"ItemsSource="{Binding SelectedItem.Invoices, ElementName=cboCustomers}"/>
如果您还希望在第一个组合框中选择某些内容之前禁用第二个组合框,您可以通过转换器将第二个组合框的IsEnabled 属性绑定到第一个组合框的SelectedItem 属性。
将此类添加到您的项目中:
public class NullToBooleanConverter : IValueConverter {
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture) {
if (targetType == typeof(Boolean))
return value != null;
throw new NotSupportedException("Value converter can only convert to Boolean type.");
}
public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture) {
throw new NotSupportedException("Value converter cannot convert back.");
}
}
将此类的实例添加到用户控件的资源字典中(local 是转换器命名空间的命名空间标记):
<UserControl.Resources>
<local:NullToBooleanConverter x:Key="NullToBooleanConverter"/>
</UserControl.Resources>
然后您可以将其添加到第二个组合框:
IsEnabled="{Binding SelectedItem, ElementName=cboCustomers, Converter={StaticResource NullToBooleanConverter}}"