2009-10-01 140 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}}"