Datagrid错误BindingExpression NotSupportedException

时间:2015-05-07 08:00:33

标签: c# wpf datagrid mvvm-light

我正在尝试使用动态集合绑定创建datagrid。为了澄清我的意思,请看下面的代码片段:

<UserControl x:Class="Customizing.Views.UsersView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ig="http://schemas.infragistics.com/xaml"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:mvvm="http://www.galasoft.ch/mvvmlight"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             DataContext="{Binding LoginsVm, Source={StaticResource Locator}}">
    <Grid x:Name="_users">
        <ig:ThemeManager.Theme>
            <ig:Office2013Theme StyleMicrosoftControls="True" />
        </ig:ThemeManager.Theme>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TabControl Grid.Row="0" ItemsSource="{Binding Users}">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding Location}" FontSize="16" FontWeight="Bold" />
                </DataTemplate>
            </TabControl.ItemTemplate>


            <TabControl.ContentTemplate>
                <DataTemplate>
                    <DataGrid ItemsSource="{Binding Data}" SelectedItem="{Binding Path=DataContext.SelLogin, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" FontSize="14" CanUserResizeColumns="False"
                              CanUserResizeRows="False" CanUserReorderColumns="False" ColumnWidth="100" SelectedValue="{Binding Path=DataContext.FocusLogin, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                              IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" SelectionMode="Single">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="CellEditEnding">
                                <mvvm:EventToCommand Command="{Binding Path=DataContext.LeaveEditMode, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/> 
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Username"
                                                Binding="{Binding User, UpdateSourceTrigger=PropertyChanged}"
                                                Width="*" />
                            <DataGridTextColumn Header="Password"
                                                Binding="{Binding Password, UpdateSourceTrigger=PropertyChanged}"
                                                Width="*" />
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
</UserControl>

viewmodel类:

using System.Collections.ObjectModel;
using Customizing.Models;
using Customizing.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace Customizing.ViewModel
{
    public class LoginsViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;

        public LoginsViewModel(IDataService dataService)
        {
            _dataService = dataService;
            _dataService.QueryLogins((logins, error) => { Users = logins; });

            LeaveEditMode = new RelayCommand(_leaveEditMode);
        }

        public ObservableCollection<Logins> Users { get; set; }
        public Login SelLogin { get; set; }
        public int FocusLogin { get; set; }
        public RelayCommand LeaveEditMode { get; set; }

        private void _leaveEditMode()
        {
        }
    }
}

当我运行应用程序时,我遇到了运行时异常:

System.Windows.Data Error: 23 : Cannot convert 'Customizing.Models.Login' from type 'Login' to type 'System.Int32' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from Customizing.Models.Login.
   at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
   at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'
System.Windows.Data Error: 7 : ConvertBack cannot convert value 'Customizing.Models.Login' (type 'Login'). BindingExpression:Path=DataContext.FocusLogin; DataItem='UsersView' (Name=''); target element is 'DataGrid' (Name=''); target property is 'SelectedValue' (type 'Object') NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from Customizing.Models.Login.
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.DefaultValueConverter.ConvertFrom(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
'Customizing.vshost.exe' (CLR v4.0.30319: Customizing.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationProvider\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationProvider.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
System.Windows.Data Error: 23 : Cannot convert 'Customizing.Models.Login' from type 'Login' to type 'System.Int32' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from Customizing.Models.Login.
   at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
   at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'
System.Windows.Data Error: 7 : ConvertBack cannot convert value 'Customizing.Models.Login' (type 'Login'). BindingExpression:Path=DataContext.FocusLogin; DataItem='UsersView' (Name=''); target element is 'DataGrid' (Name=''); target property is 'SelectedValue' (type 'Object') NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from Customizing.Models.Login.
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.DefaultValueConverter.ConvertFrom(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'

我做错了什么?捆绑?

0 个答案:

没有答案