我有一个名为IGdaily的表格,其中包含Field Trans_Category。我想绑定一个显示枚举的DataGridComboBoxColumn,并将其int值绑定到datagrid单元格Trans_Category。 我的Enum
public enum Enm_Purch_Ret : short
{
Purchase = 1,
Sale = 2,
Return = 3
}
Viewmodel Vm_Purchase
public class Vm_Purchase : INotifyPropertyChanged
{
private IGoldEntities db = new IGoldEntities();
public ObservableCollection<IGdaily> Vm_IGdaily { get; set; }
public ObservableCollection<Enm_Purch_Ret> Vm_Enum_P_R { get; set; }
public Vm_Purchase()
{
Vm_IGdaily = new ObservableCollection<IGdaily>();
Vm_Enum_P_R = new ObservableCollection<Enm_Purch_Ret>(Enum.GetValues(typeof(Enm_Purch_Ret)).Cast<Enm_Purch_Ret>().ToList());
}
public ObservableCollection<IGdaily> IGDailys
{
get { return Vm_IGdaily; }
set { Vm_IGdaily = value; NotifyPropertyChanged(); }
}
public ObservableCollection<Enm_Purch_Ret> Enm_Purch_Rets
{
get { return Vm_Enum_P_R; }
set { Vm_Enum_P_R = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
在XAML中
<Window.DataContext>
<local:Vm_Purchase/>
</Window.DataContext>
<DataGrid x:Name="DG" ItemsSource="{Binding IGDailys}" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="Cell" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding GroupName}" Header="Name" Width="200"/>
<DataGridComboBoxColumn Header="Item/Metal" SelectedValueBinding="{Binding Trans_Category}" SelectedValuePath="{Binding Path=Enm_Purch_Rets, StringFormat='\{0:D\}'}" DisplayMemberPath="Enm_Purch_Ret">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.Enm_Purch_Rets, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.Enm_Purch_Rets , RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTextColumn Binding="{Binding Trans_Category}" ClipboardContentBinding="{x:Null}" FontSize="14" Header="Metal Id" Width="100"/>
</DataGrid.Columns>
</DataGrid>
public partial class IGdaily
{
public int GDaily_Id { get; set; }
public int DailyMast_Id { get; set; }
public int ItemGroup_Id { get; set; }
public int Item_Id { get; set; }
public int Trans_Category { get; set; }
}
请帮助我在绑定方面的错误。我是MVVM模式的新手。 请解释 感谢
答案 0 :(得分:0)
您无法将int
属性设置为Enm_Purch_Ret
值,因为这两种类型之间没有隐式转换。
但您可以使用转换器类为您进行转换:
public class EnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Enm_Purch_Ret e = (Enm_Purch_Ret)value;
return (int)e;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int i = (int)value;
return (Enm_Purch_Ret)i;
}
}
<强>用法:强>
<DataGrid x:Name="DG" ItemsSource="{Binding IGDailys}" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="Cell" >
<DataGrid.Resources>
<local:EnumConverter x:Key="conv" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding GroupName}" Header="Name" Width="200"/>
<DataGridComboBoxColumn Header="Item/Metal"
SelectedItemBinding="{Binding Trans_Category, Converter={StaticResource conv}}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.Enm_Purch_Rets, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.Enm_Purch_Rets , RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTextColumn Binding="{Binding Trans_Category}" ClipboardContentBinding="{x:Null}" FontSize="14" Header="Metal Id" Width="100"/>
</DataGrid.Columns>
</DataGrid>