Wpf DataGrid ComboBox 列

遇到的问题

  1. 最开始找到的例子要写 Convert, 感觉和 Vue-Elment 的差别比较大后面找到类似与 Vue-Element UI 的写法,
  2. 开始时数值不更新

关键代码

Xaml 复制代码
   <DataGridTemplateColumn Header="Digit" Width="100">
       <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
               <ComboBox 
                   ItemsSource="{Binding DataContext.DigitTypes, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                 SelectedValue="{Binding DigitType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
              
                 SelectedValuePath="Id"
                 DisplayMemberPath="Name"/>
           </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>

完整代码

CSharp 复制代码
  public class EnumDesp
  {
     public int Id { get; set; }
     public string Name { get; set; }
  }

public class MainWindowViewModel : BindableBase
{
    private string _title = "Prism Application";
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }

    }

    public ObservableCollection<RealDigitViewModel> Digits { get; set; }=new ObservableCollection<RealDigitViewModel>();
    public ObservableCollection<EnumDesp> DigitTypes { get; set; }

    public MainWindowViewModel()
    {
     
        DigitTypes = DigitTypeConverter.DigitTypes;
        this.Digits.Add(new RealDigitViewModel()
        {
            Name = "Abc",
            DigitType = 1
        });

        this.Digits.Add(new RealDigitViewModel()
        {
            Name = "Def",
            DigitType = 2
        });
    }

 
}
xmal 复制代码
<Window x:Class="BlankApp1.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Command="{Binding MyCommand}" Content="Me" ></Button>
       <DataGrid Grid.Row="1" ItemsSource="{Binding Digits}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            
                <DataGridTemplateColumn Header="Digit" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox 
                                ItemsSource="{Binding DataContext.DigitTypes, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                              SelectedValue="{Binding DigitType,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                           
                              SelectedValuePath="Id"
                              DisplayMemberPath="Name"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
相关推荐
_oP_i6 小时前
WinForms 项目里生成时选择“首选目标平台 32 位导致有些电脑在获取office word对象时获取不到
c#·office
要记得喝水6 小时前
C#某公司面试题(含题目和解析)--1
开发语言·windows·面试·c#·.net
冷冷的菜哥7 小时前
ASP.NET Core文件分片上传
c#·asp.net·asp.net core·文件分片上传
上位机付工9 小时前
2025年了,学C#上位机需要什么条件
c#·上位机·西门子
c#上位机9 小时前
wpf之Border
c#·wpf
SunflowerCoder9 小时前
WPF迁移avalonia之图像处理(一)
图像处理·wpf·avalonia
周杰伦fans10 小时前
WPF中的DataContext以及常见的绑定方式
wpf
almighty2716 小时前
C# 阿里云 OSS 图片上传步骤及浏览器查看方法
c#·图片上传·c# 阿里云 oss·阿里云 oss