2009-10-01 137 views
3

我想要做这样的事情的结果:Silverlight的绑定组合框到另一个组合框

<combobox x:Name="cboCustomers" ItemsSource="{Binding Path=Data.Customers}"/> 
<combobox x:Name="cboInvoices"ItemsSource="{cboCustomers.SelectedItem.Invoices}"/> 

任何人都知道的方式做这样的事情在Silverlight 3?我确信这里有一些关于它的信息,但是我在与Google形成这个问题方面运气不好。

回答

4

你需要指定第二结合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}}"