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>
相关推荐
宝桥南山14 分钟前
DeepSeek - 尝试一下GitHub Models中的DeepSeek
microsoft·ai·微软·c#·github·.net
code bean3 小时前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf
lljss20207 小时前
C# 一个解决方案放一个dll项目,一个dll测试项目 ,调试dll项目的源码
c#
沉到海底去吧Go8 小时前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
lph19728 小时前
自定义事件wpf
wpf
code bean12 小时前
【WPF】从普通 ItemsControl 到支持筛选的 ItemsControl:深入掌握 CollectionViewSource 用法
wpf
ghost14315 小时前
C#学习第27天:时间和日期的处理
开发语言·学习·c#
jason成都15 小时前
c#压缩与解压缩-SharpCompress
开发语言·c#
傻啦嘿哟16 小时前
从零开始:用Tkinter打造你的第一个Python桌面应用
开发语言·c#
CodeCraft Studio17 小时前
PDF处理控件Aspose.PDF教程:在 C# 中更改 PDF 页面大小
前端·pdf·c#