将silverlight组合框绑定到另一个组合框的结果

我想做这样的事情:

<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}}"

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...