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>
相关推荐
pchmi1 小时前
C# OpenCV机器视觉:红外体温检测
人工智能·数码相机·opencv·计算机视觉·c#·机器视觉·opencvsharp
m0_748248022 小时前
【MySQL】C# 连接MySQL
数据库·mysql·c#
苏克贝塔3 小时前
WPF5-x名称空间
wpf
oulaqiao5 小时前
语言集成查询LINQ
c#·linq
xcLeigh6 小时前
WPF实战案例 | C# WPF实现大学选课系统
开发语言·c#·wpf
one9966 小时前
.net 项目引用与 .NET Framework 项目引用之间的区别和相同
c#·.net·wpf
xcLeigh6 小时前
WPF基础 | WPF 布局系统深度剖析:从 Grid 到 StackPanel
c#·wpf
军训猫猫头16 小时前
52.this.DataContext = new UserViewModel(); C#例子 WPF例子
开发语言·c#·wpf
AI+程序员在路上20 小时前
C#调用c++dll的两种方法(静态方法和动态方法)
c++·microsoft·c#