【问题标题】:Bind silverlight combobox to the result of another combobox将 silverlight 组合框绑定到另一个组合框的结果
【发布时间】:2009-10-01 22:17:55
【问题描述】:

我想做这样的事情:

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

任何人都知道在 Silverlight 3 中执行此类操作的方法吗?我确信那里有一些关于它的信息,但我在 Google 提出这个问题时运气不好。

【问题讨论】:

    标签: silverlight data-binding xaml


    【解决方案1】:

    您需要在第二个绑定上指定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】:

      【讨论】:

        猜你喜欢
        • 2016-08-23
        • 2012-07-24
        • 2015-10-19
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-11
        • 1970-01-01
        相关资源
        最近更新 更多