WPF Datagrid DataTemplate ComboBo不捕获Viewmodel中的更改

时间:2014-07-08 16:56:49

标签: c# wpf xaml mvvm datagrid

到目前为止,我已经得到了很多关于SO的帮助,但是我已经达到了我已经陷入困境的步伐。我可以将它用于单个组合框,但是当我在DataGridTemplate中执行此操作时,它不会让人感到愤怒。我希望FXCItem属性可以从组合框中获取任何值,但它总是返回null。此外,我的ConfigItems属性中的'SET'方法根本没有被抛出。我在这里错过了什么?提前谢谢!

ViewModel&模型(这是在按钮'click'事件中填充的):

   class ConfigViewModel : ViewModelEntity
{

    public Settings Settings { get; set; }
    public List<String> FileTypes { get; set; }
    public List<String> Delimiters { get; set; }
    public List<string> FXCFields { get; set; }

    protected ObservableCollection<ConfigItem> _ConfigItems = new ObservableCollection<ConfigItem>();
    public ObservableCollection<ConfigItem> ConfigItems
    {
        get { return _ConfigItems; }
        set
        {
            if (_ConfigItems != value)
            {
                _ConfigItems = value;
                NotifyPropertyChanged("ConfigItems");
            }

        }
    }}

class ConfigItem
{
    public string InputField { get; set; }
    public string FXCField { get; set; }

    public ConfigItem(string InputFieldName)
    {
        this.InputField = InputFieldName;
    }
}

XAML:

<DataGrid x:Name="dgConfig" HorizontalAlignment="Left" Margin="40.66,98,0,0" VerticalAlignment="Top" Height="300.486" Width="323" AutoGenerateColumns="False" ItemsSource="{Binding ConfigItems}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding InputField}" Header="Input Fields"/>
        <DataGridTemplateColumn Header="FXC Fields">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding Path=DataContext.FXCFields, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                        SelectedItem="{Binding Path=FXCItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding Path=DataContext.FXCFields, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                        SelectedItem="{Binding Path=FXCItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

1 个答案:

答案 0 :(得分:0)

尝试

NotifyPropertyChanged("FXCFields");