Boolean dataGridComboBoxColumn显示是/否

时间:2015-10-27 07:27:34

标签: c# wpf datagridcomboboxcolumn

我有一个与Collection绑定的dataGrid。对于两列(A和B),数据为1或0。 相反,我想在comboBoxColumn中显示Yes或No,当然如果选择'yes',则保存为1,为'no'保存为0。

我该怎么做?

查看

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

更新

我尝试了两种方法,但它不起作用。

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

1 个答案:

答案 0 :(得分:3)

我可以建议你解决这个问题的下一个问题。请检查一下。  1. ComboBox 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>

你可以在这个堆栈溢出查询链接上看到整个解决方案: How to Display and select items in a Datagrid ComboBox with WPF C#, using MVVM

的问候,