我想做这样的事情:
<combobox x:Name="cboCustomers" ItemsSource="{Binding Path=Data.Customers}"/> <combobox x:Name="cboInvoices"ItemsSource="{cboCustomers.SelectedItem.Invoices}"/>
有人知道在Silverlight 3中做这样的事情吗?我确信有一些关于它的信息,但我在与Google形成问题时运气不佳.
解决方法
您需要在第二个绑定上指定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,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}}"