2015-10-27 57 views
1

我有一個數據網格與集合綁定。對於兩列(A和B),數據爲1或0. 相反,我想在comboBoxColumn中顯示Yes或No,當然如果選擇'yes',則保存1,否則爲0。布爾dataGridComboBoxColumn顯示是/否

我該怎麼做?

查看

<DataGrid x:Name="dataGrid" ItemsSource="{Binding Collection}" AutoGenerateColumns="False" 
           SelectedItem="{Binding Selected, Mode=TwoWay}"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Header="id" Binding="{Binding idproduct, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden"/> 
         <DataGridTextColumn Header="Ref Supplier" Binding="{Binding refsup, UpdateSourceTrigger=PropertyChanged}"/> 
         <DataGridTextColumn Header="Description" Binding="{Binding description, UpdateSourceTrigger=PropertyChanged}"/> 
         <DataGridComboBoxColumn Header="Unit" 
                 ItemsSource="{Binding Collection, Source={StaticResource unit}}" 
                 DisplayMemberPath="unit1" 
                 SelectedValueBinding="{Binding unit}" 
                 SelectedValuePath="idunit"/> 
         <DataGridTextColumn Header="Price/MOQ" Binding="{Binding priceMOQ, UpdateSourceTrigger=PropertyChanged}"/> 
         <DataGridTextColumn Header="A" Binding="{Binding A, UpdateSourceTrigger=PropertyChanged}"/> 
         <DataGridTextColumn Header="B" Binding="{Binding B, UpdateSourceTrigger=PropertyChanged}"/> 
        </DataGrid.Columns> 
    </DataGrid> 

視圖模型

public class ProductViewModel : ViewModelBase 
    { 


     public Context ctx = new Context(); 

     private ObservableCollection<product> collection; 

     public ObservableCollection<product> Collection 
     { 
      get 
      { 
       if (collection == null) 
       { 
        Get(); 
       } 
       return collection; 
      } 
      set 
      { 
       collection = value; 
       OnPropertyChanged("Collection"); 
      } 
     } 

     #region "Command" 

    } 

型號

public partial class product 
    { 
     public int idproduct { get; set; } 
     public string refsup { get; set; } 
     public string description { get; set; } 
     public Nullable<int> unit { get; set; } 
     public sbyte larot { get; set; } 
     public sbyte lasand { get; set; } 


     public virtual foodSupplier foodSupplier { get; set; } 
     public virtual unit unit1 { get; set; } 
    } 

UPD吃了

我試過了兩種方法,但它不起作用。

<Page.Resources> 
     <hlp:YesNoToBooleanConverter x:Key="YesNoToConverter" /> 
</Page.Resources> 

<DataGridTemplateColumn> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <ComboBox ItemsSource="{Binding A}" 
              SelectedValue="{Binding A}"> 
            <ComboBox.ItemTemplate> 
             <DataTemplate> 
              <TextBlock Name="TextBlock" Text="No" /> 
              <DataTemplate.Triggers> 
               <DataTrigger Binding="{Binding}" Value="1"> 
                <Setter TargetName="TextBlock" Property="Text" Value="Yes" /> 
               </DataTrigger> 
              </DataTemplate.Triggers> 
             </DataTemplate> 
            </ComboBox.ItemTemplate> 
           </ComboBox> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 
        <DataGridTemplateColumn> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <ComboBox ItemsSource="{Binding B}" 
              SelectedValue="{Binding B}"> 
            <ComboBox.ItemTemplate> 
             <DataTemplate> 
              <TextBlock Text="{Binding Converter={StaticResource YesNoToConverter}}" /> 
             </DataTemplate> 
            </ComboBox.ItemTemplate> 
           </ComboBox> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 



public class YesNoToBooleanConverter : IValueConverter 
    { 

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      byte inValue = System.Convert.ToByte(value); 
      string outValue = (inValue == 1) ? "Yes" : "No"; 
      return outValue; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 

      switch (value.ToString().Trim().ToLower()) 
      { 
       case "yes": 
        return 1; 
       case "no": 
        return 0; 
       default: 
        return 0; 
      } 
     } 
    } 
+0

你能分享你的產品型號 –

回答

1

我可以建議你下一個這個問題的確切解決方案。請檢查一下。 1.組合框XAML代碼:

<DataGridComboBoxColumn Header="ActivityStatus" SelectedItemBinding="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
      <DataGridComboBoxColumn.ItemsSource> 
       <x:Array Type="system:Boolean"> 
        <system:Boolean>True</system:Boolean> 
        <system:Boolean>False</system:Boolean> 
       </x:Array> 
      </DataGridComboBoxColumn.ItemsSource> 
     </DataGridComboBoxColumn> 

你CA看到這個堆棧溢出的查詢鏈接上的整體解決方案: How to Display and select items in a Datagrid ComboBox with WPF C#, using MVVM

關於,

+0

謝謝你,它工作正常。 – Cantinou

+0

@Cantinou不客氣! – Ilan