2012-06-27 35 views
0

在我的WPF应用程序,我有一个DataGrid如下收集到布尔转换器

      <Custom:DataGrid x:Name="dg_nba" IsEnabled="{Binding Iseditmode}" SelectionMode="Single" ItemsSource="{Binding Products}" Style="{DynamicResource myDataGridStyle}" IsReadOnly="True" AutoGenerateColumns="False" CanUserAddRows="False" ColumnWidth="*"> 
         <Custom:DataGrid.Columns> 
          <Custom:DataGridTextColumn x:Name="dgt_nba_id" Header="Id" Binding="{Binding ID}" MaxWidth="40"/> 
          <Custom:DataGridTextColumn x:Name="dgt_nba_name" Binding="{Binding Name}" Header="Name"/> 
          <Custom:DataGridTemplateColumn x:Name="dgtc_nba_incl" Header="Include" MaxWidth="50"> 
           <Custom:DataGridTemplateColumn.CellTemplate > 
            <DataTemplate> 
              <CheckBox HorizontalAlignment="Center" Style="{DynamicResource myCheckBoxStyle}"/> 
            </DataTemplate> 
           </Custom:DataGridTemplateColumn.CellTemplate> 
          </Custom:DataGridTemplateColumn> 
         </Custom:DataGrid.Columns> 
        </Custom:DataGrid> 

我已经绑定DataGrid的ID,与产品的默认集合名称列。我有另一个产品列表收集,其中只包含产品,现在我需要检查复选框,如果列表包含产品。

有人可以帮助我的集合布尔转换器。我尽了最大的努力,但还没有把握好。

在此先感谢。

+0

此外,您可能需要查看不同的解决方案,因为IValueConverter只能使用一个参数,您需要两个:需要查找的集合和值。 –

回答

0

如果你想使用价值转换,我会建议你试试IMultiValueConverter。您可以尝试将其他集合作为值并将您要查找的值作为传递给转换器的两个不同值。为了使其工作,您应该:

  • 使您可以在XAML中访问代码隐藏变量。你可以在这里找到更多关于一些方法的信息:Access codebehind variable in XAML
  • implement IMultiValueConverter。这可能取决于你的应用程序(如类型,你AE使用收集的)的一些细节,但它可能看起来或多或少是这样的:

    class ICollectionToBoolConverter : IMultiValueConverter 
    { 
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
         try 
         { 
          //verify if appropriate number of values is bound 
          if (values != null && values.Length == 2) 
          { 
           List<Product> productsList = (values[0] as List<Product>); 
    
           //if converter is used with appropriate collection type 
           if (productsList != null) 
           { 
            //if there is object ID specified to be found in the collection 
            if (values[1] != null) 
            { 
             int objectToFindId = (int)values[1]; 
    
             //return information if the collection contains an item with ID specified in parameter 
             return productsList.Any(p => p.ID == objectToFindId); 
            } 
           } 
          } 
    
          //return false if object is not found or converter is used inappropriately 
          return false; 
         } 
         catch 
         { 
          return false; 
         } 
        } 
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
        { 
         throw new NotImplementedException(); 
        } 
    } 
    
  • 把你的新创建的转换器中的WindowResources(或UserControl ),其中使用转换器DataGrid所在

    <c:ICollectionToBoolConverter x:Key="collectionToBoolConverter" /> 
    
  • 绑定的CheckBox,它可以依靠你使用暴露在另一个集合(在这个答案的第一步提到的)具体的方式。但是,它可能类似于此:

    ... 
    <Custom:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <CheckBox HorizontalAlignment="Center" Style="{DynamicResource myCheckBoxStyle}"> 
          <CheckBox.IsChecked> 
           <MultiBinding Converter="{StaticResource collectionToBoolConverter}"> 
            <Binding ElementName="layoutRoot" Path="Parent.MyCollectionName" /> 
            <Binding Path="ID" /> 
           </MultiBinding> 
          </CheckBox.IsChecked> 
         </CheckBox> 
        </DataTemplate> 
    </Custom:DataGridTemplateColumn.CellTemplate> 
    ... 
    

我没有测试过这一点,所以如果你有任何的这些任务的任何问题,让我知道,我可以尽力帮助你。

+0

Thanks.I写了一个类似的转换器,但绑定有一些问题。我发现你的控制器更简单,我使用它。 – maran87

+0

我可以让它成为双向绑定吗?如果是这样,我应该使用convertback来达到这个目标吗? – maran87

+0

我现在坚持使用convertback,因为我使用它的两种方式。这个转换器是可以转换的吗? – maran87

0

在这种情况下,您可能会更好地计算ViewModel中的IsChecked值(您要绑定的对象)。如果您从VM中公开描述性属性(只读:HasDesiredProduct),则可以在向集合添加/删除项目时调整该属性,并使复选框以只读方式反映内部逻辑。