WPF将DataGrid组合框选中的项绑定到所选DataGridRow的datacontext

时间:2016-12-23 10:14:13

标签: c# .net wpf combobox datagrid

我有一个DataGridTemplateColumn组合框,它绑定到一个单独的ItemsSource似乎通过将这个组合框绑定到另一个源它改变了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>

C#:

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:无法转换'{NewItemPlaceholder}'   从“NamedObject”类型到“en-US”文化的“WorldData”类型   默认转换;考虑使用Binding的Converter属性。   NotSupportedException:'System.NotSupportedException:TypeConverter   无法从MS.Internal.NamedObject转换。在   System.ComponentModel.TypeConverter.GetConvertFromException(对象   价值)   System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext   上下文,CultureInfo文化,对象价值)   MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o,Type   destinationType,DependencyObject targetElement,CultureInfo culture,   Boolean isForward)'抛出异常:'System.NotSupportedException'   在PresentationFramework.dll

似乎我应该使用类型转换器,但似乎我不应该从我能够在SO上找到的那么少来做

我想现在我只是放弃并为组合框添加一个单独的框并绑定到Datagrid的选定项目。我觉得它不直观,所以请你有任何聪明的想法。

1 个答案:

答案 0 :(得分:1)

这是我的完整测试应用,可将所选国家/地区保存到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; } } } } 则无法正常工作。

{{1}}