布尔 dataGridComboBoxColumn 显示 Yes/No

Boolean dataGridComboBoxColumn display Yes/No

我有一个与集合绑定的数据网格。对于两列(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>

ViewModel

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.组合框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>

您可以在这个堆栈溢出查询中看到完整的解决方案 link: .

问候,