代码之家  ›  专栏  ›  技术社区  ›  JasonRShaver

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

  •  3
  • JasonRShaver  · 技术社区  · 15 年前

    我想这样做:

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

    有人知道在Silverlight 3中做这种事情的方法吗?我确信有一些关于它的信息,但是我和谷歌在形成这个问题上运气不好。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Martin Liversage    15 年前

    您需要指定 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}}"
    
        2
  •  3
  •   gn22    15 年前