如何让DataGridComboBoxColumn显示用户电话号码(现在它显示空白的组合框)?
编辑:以下代码已更新,现已有效。
<Window x:Class="DataGridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid
x:Name="myDataGrid"
Grid.Row="1"
AutoGenerateColumns="False"
CanUserAddRows="True"
ItemsSource="{Binding PersonList}"
>
<DataGrid.Columns>
<DataGridTextColumn
Header="LastName"
Binding="{Binding LastName}"/>
<DataGridTextColumn
Header="FirstName"
Binding="{Binding FirstName}" />
<DataGridComboBoxColumn
Header="Phone Number"
>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding PhoneNumbers}" />
<Setter Property="SelectedValue" Value="{Binding SelectedPhoneNumber}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding PhoneNumbers}" />
<Setter Property="SelectedValue" Value="{Binding SelectedPhoneNumber}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
//代码隐藏
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DataGridTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<Person> PersonList { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
PersonList = new ObservableCollection<Person>();
Person jjim = new Person() { FirstName = "jimmy", LastName = "jim" };
jjim.PhoneNumbers.Add("123-4567");
jjim.PhoneNumbers.Add("234-5678");
Person mmark = new Person() { FirstName = "mike", LastName = "mark" };
mmark.PhoneNumbers.Add("345-6789");
mmark.PhoneNumbers.Add("456-7890");
PersonList.Add(jjim);
PersonList.Add(mmark);
}
}
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> PhoneNumbers { get; set; }
private string selectedPhoneNumber;
public string SelectedPhoneNumber
{
get
{
return this.selectedPhoneNumber;
}
set
{
this.selectedPhoneNumber = value;
RaisePropertyChanged("SelectedPhoneNumber");
}
}
public Person()
{
PhoneNumbers = new ObservableCollection<string>();
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string firstName;
private string lastName;
public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
RaisePropertyChanged("FirstName");
}
}
public string LastName
{
get
{
return this.lastName;
}
set
{
this.lastName = value;
RaisePropertyChanged("LastName");
}
}
}
}
答案 0 :(得分:1)
请看一下我的问题:DataGridComboBoxColumn is empty
似乎是同样的问题。 DataGridComboBoxColumn
有点儿错误。
答案 1 :(得分:1)
DataGridColumn
不是视觉元素,没有DataContext
,因此您的绑定不起作用。您可以通过为ElementStyle
指定EditingElementStyle
和DataGridComboBoxColumn
来轻松解决此问题:
<DataGridComboBoxColumn Header="Phone Number">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding PhoneNumbers}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding PhoneNumbers}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
这是有效的,因为最终由ComboBox
添加到可视树中的实际DataGridComboBoxColumn
元素以及应用的样式始终具有DataContext
。
有关如何在WPF中绑定ComboBox
数据的更多示例,请参阅以下链接:https://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82#content