별도의 ItemsSource에 바인딩하는 DataGridTemplateColumn 콤보 상자가 있습니다.이 콤보 상자를 다른 소스에 바인딩하면 DataContext가 변경되는 것 같습니다.
따라서 콤보 상자에서 선택한 항목의 값을 DataGrid에서 선택한 행의 DataContext에 바인딩하는 데 문제가 있습니다.
XAML :
<DataGrid HorizontalAlignment="Left" ItemsSource="{Binding Path=WorldDataList}" SelectedItem="{Binding SelectedWorldData}">
<DataGridTemplateColumn Header="Country" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}},Path=DataContext.Countries}"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=SelectedItem.Country}"
SelectedIndex="0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
씨#:
class WorldDataViewModel : ObservableObject
{
private ObservableCollection<WorldData> _worldDataList = new ObservableCollection<WorldData>();
public ObservableCollection<WorldData> WorldDataList
{
get { return _worldDataList; }
set
{
Set(ref _worldDataList, value);
}
}
public List<string> Countries {get;set;}
private WorldData worldData;
private WorldData SelectedWorldData
{
get{return worldData;}
set
{
Set(ref worldData, value);
}
}
}
class WorldData : ObservableObject
{
private string country;
public string Country
{
get{return country;}
set
{
Set(ref country, value);
}
}
예외가 발생합니다.
System.Windows.Data 오류 : 23 : 기본 변환을 사용하는 'en-US'문화권의 '{NewItemPlaceholder}'를 'NamedObject'형식에서 'WorldData'형식으로 변환 할 수 없습니다. Binding의 Converter 속성 사용을 고려하십시오. NotSupportedException : 'System.NotSupportedException : TypeConverter는 MS.Internal.NamedObject에서 변환 할 수 없습니다. System.ComponentModel.TypeConverter.GetConvertFromException (Object value) at System.ComponentModel.TypeConverter.ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, Object value) at MS.Internal.Data.DefaultValueConverter.ConvertHelper (Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward) '예외 발생 : PresentationFramework.dll의'System.NotSupportedException '
유형 변환기를 사용해야하는 것 같지만 내가 찾을 수 있었던 아주 작은 것에서 이것을하지 말아야하는 것 같습니다.
지금은 포기하고 콤보 상자에 별도의 상자를 추가하고 Datagrid의 선택한 항목에 바인딩 할 것입니다. 직관적이지 않으므로 영리한 아이디어가 있으시면 제발.
다음은 선택한 국가를 WorldData
뷰 모델에 다시 저장하는 전체 테스트 앱입니다 . 포함하지 않는 UpdateSourceTrigger
에서이 SelectedItem
작동하지 않습니다.
<Window
x:Class="ComboboxInDataGrid.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"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Grid>
<DataGrid x:Name="me"
HorizontalAlignment="Left"
AutoGenerateColumns="False"
CanUserAddRows="False"
ItemsSource="{Binding Path=WorldDataList}"
SelectedItem="{Binding SelectedWorldData}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Country">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}},
Path=DataContext.Countries}"
SelectedItem="{Binding Path=Country,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
using System.Collections.Generic;
using System.Windows;
namespace ComboboxInDataGrid
{
public partial class MainWindow : Window
{
public MainWindow ()
{
DataContext = new WorldDataViewModel ();
InitializeComponent ();
}
}
class WorldDataViewModel
{
public WorldDataViewModel ()
{
var c1 = "USA";
var c2 = "Canada";
var c3 = "Brasil";
var c4 = "Brasfsdfsil";
Countries = new List<string> ();
Countries.Add (c1);
Countries.Add (c2);
Countries.Add (c3);
Countries.Add (c4);
_worldDataList.Add (new WorldData { Index = 1, Country = c2 });
_worldDataList.Add (new WorldData { Index = 2, Country = c3 });
_worldDataList.Add (new WorldData { Index = 3, Country = c4 });
}
private List<WorldData> _worldDataList = new List<WorldData> ();
public List<WorldData> WorldDataList
{
get
{
return _worldDataList;
}
set
{
_worldDataList = value;
}
}
public List<string> Countries { get; set; }
private WorldData worldData;
public WorldData SelectedWorldData
{
get
{
return worldData;
}
set
{
worldData = value;
}
}
}
public class WorldData
{
public int Index { get; set; }
private string country;
public string Country
{
get { return country; }
set
{
country = value;
}
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다