ComboBox가 포함 된 사용자 정의 DataGrid 열을 만들고 싶습니다. 콤보 박스의 ItemSource는 enum에 바인딩되고 combobox의 selecteditem은 콜렉션 요소에서 선택된 enum 값에 바인딩됩니다. 여기DataGridTemplateColumn의 ComboBox가 SelectedItem을 표시하지 않습니다.
코드 숨김
public partial class MainWindow : Window
{
AnimalObservableCollection animals;
public MainWindow()
{
InitializeComponent();
animals = (AnimalObservableCollection)this.FindResource("animals");
animals.Add(new Animal(AnimalType.horse,"Rex"));
}
}
public enum AnimalType { dog, cat, horse };
class AnimalObservableCollection : ObservableCollection<Animal>
{
public AnimalObservableCollection()
: base()
{
}
}
class Animal
{
private AnimalType animalType;
private string name;
public Animal()
{
}
public Animal(AnimalType animalType_, string name_)
{
animalType = animalType_;
name = name_;
}
public AnimalType AnimalType
{
get
{
return animalType;
}
set
{
animalType = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
문제는 콤보 상자가 포커스를 잃을 때, selectedItem가이 DataGrid`s 셀에 미리 보여줍니다 및이 셀이 비어 유지되지 않고 있다는 점이다
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:AnimalObservableCollection x:Key="animals">
</local:AnimalObservableCollection>
<ObjectDataProvider x:Key="animalEnum" MethodName="GetValues"
ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:AnimalType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" Margin="12,12,12,101" Name="dgAnimals" CanUserAddRows="True" CanUserDeleteRows="True" DataContext="{StaticResource animals}" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"></DataGridTextColumn>
<DataGridTemplateColumn Header="Animal type">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource animalEnum}}" SelectedItem="{Binding Path=AnimalType}"></ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
코드
입니다. 콤보 박스의 selecteditem을 표시하기 위해 AnimalType 열의 셀을 만드는 방법?내가 DataGridComboBoxColumn를 사용하지만, 미래에 좀 더 많은 기능을 추가 할 DataGridTemplateColumn를 사용하려면 I`d 때 그것은 완벽하게 작동합니다.
이것은 많은 도움이되었습니다. 감사 –