C# 指向其第一个属性绑定对象的ComboBox的第二个绑定属性

C# 指向其第一个属性绑定对象的ComboBox的第二个绑定属性,c#,wpf,binding,datagridtemplatecolumn,C#,Wpf,Binding,Datagridtemplatecolumn,我花了一个星期的时间试图解决这个问题,但没有成功!我在搜索和阅读许多页面,包括: 我想做什么 我在SQL LocalDb中有一个域类和一个表,名称为TermType(您可以在下面看到它的代码),有5个属性: TermTypeId 字体名 描述 起始日期 结束日期 我的应用程序读取TermType表,它应该在DataGrid中显示它们,如下所示: 但它没有显示每个术语类型的开始/结束日期,因为它未能以正确的方式绑定StartDate/EndDate属性!我还在输出窗口中收到此错误消息

我花了一个星期的时间试图解决这个问题,但没有成功!我在搜索和阅读许多页面,包括:

我想做什么

我在SQL LocalDb中有一个域类和一个表,名称为TermType(您可以在下面看到它的代码),有5个属性:

  • TermTypeId
  • 字体名
  • 描述
  • 起始日期
  • 结束日期
我的应用程序读取
TermType
表,它应该在
DataGrid
中显示它们,如下所示:

但它没有显示每个术语类型的开始/结束日期,因为它未能以正确的方式绑定
StartDate
/
EndDate
属性!我还在
输出
窗口中收到此错误消息(不作为例外):

在我说更多之前!请检查与此窗口相关的我的
UserControl
xaml文件:

<UserControl x:Class="PresentationWPF.View.UserPanels.UserControlTermTypeCrud"
             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:local="clr-namespace:PresentationWPF.View.UserPanels"
             xmlns:userPanels="clr-namespace:PresentationWPF.ViewModel.Client.UserPanels"
             xmlns:converters="clr-namespace:PresentationWPF.View.Converters"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             Background="DarkSlateGray">
    <UserControl.Resources>
        <userPanels:TermTypeCrudViewModel x:Key="TermTypeCrudViewModel"/>
        <converters:IndexToMonthConverter x:Key="IndexToMonthConverter"/>
    </UserControl.Resources>

    <Grid DataContext="{StaticResource TermTypeCrudViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="9*"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <DataGrid ItemsSource="{Binding TermTypes}" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
            <!--
            <DataGrid.Resources>
                <userPanels:BindingProxy x:Key="MyProxy" Data="{Binding}"/>
            </DataGrid.Resources>
            -->
            <DataGrid.Columns>
                <DataGridTextColumn Header="Term Name" Binding="{Binding TypeName}" Width="120"/>
                <DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="*"/>

                <DataGridTemplateColumn Header="Date Range" Width="150">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                    <TextBlock  Grid.Column="0" Grid.Row="0" Text="Start: "/>
                                    <ComboBox  Grid.Column="1" Grid.Row="0" 
                                               Style="{StaticResource ComboBoxMonthNamesStyle}"
                                               SelectedIndex="{Binding StartDate,
                                                                Converter={StaticResource IndexToMonthConverter}}"
                                    />

                                    <TextBlock Grid.Column="0" Grid.Row="1" Text="End: "/>
                                    <ComboBox Grid.Column="1" Grid.Row="1" 
                                              Style="{StaticResource ComboBoxMonthNamesStyle}"
                                              SelectedIndex="{Binding EndDate, 
                                                                Converter={StaticResource IndexToMonthConverter}}"
                                    />
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>
请告诉我如何解决这个问题

我是否正确使用了MVVM和UnitOfWork

有没有更好的建议来替换App.xaml!中的MonthName类或ComboBox样式

或者您可能在我的代码中看到的任何其他问题非常感谢Advanced。

如果您需要查看/了解我的其他相关课程和。。。这是:

术语类型类:

namespace Core.BusinessLayer.Domain
{
    public partial class TermType
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public TermType()
        {
            Terms = new HashSet<Term>();
        }

        public int TermTypeId { get; set; }

        public string TypeName { get; set; }

        public string Description { get; set; }

        public DateTime? StartDate { get; set; }

        public DateTime? EndDate { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Term> Terms { get; set; }
    }
}
namespace PresentationWPF.Model
{
    public class TermTypeCrudService : IDisposable
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork(new AgsContext());
        public bool IsDbDirty = false;

        public IEnumerable<TermType> GetTermTypes() => _unitOfWork.TermTypes.GetAll();

        public void Dispose()
        {
            if (IsDbDirty)
                _unitOfWork.Complete();
            _unitOfWork.Dispose();
        }
    }
}
namespace PresentationWPF.ViewModel.Client.UserPanels
{
    public class TermTypeCrudViewModel : INotifyPropertyChanged
    {
        private readonly TermTypeCrudService _termTypeCrudService = new TermTypeCrudService();

        private ObservableCollection<TermType> _termTypes;
        public ObservableCollection<TermType> TermTypes
        {
            get
            {
                return _termTypes;
            }
            set
            {
                _termTypes = value;
                OnPropertyChanged();
            }
        }

        public TermTypeCrudViewModel()
        {
               TermTypes = new ObservableCollection<TermType>(_termTypeCrudService.GetTermTypes());
        }
        public void Dispose() => _termTypeCrudService.Dispose();

        public event PropertyChangedEventHandler PropertyChanged;
        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
namespace PresentationWPF.View.UserPanels
{
    public partial class UserControlTermTypeCrud : IUserPanelNavigation
    {
        private readonly TermTypeCrudViewModel _termTypeCrudViewModel;

        public UserControlTermTypeCrud()
        {
            InitializeComponent();

            _termTypeCrudViewModel = FindResource("TermTypeCrudViewModel") as TermTypeCrudViewModel;
        }

        public ObservableCollection<TermType> TermTypes
        {
            get => (ObservableCollection<TermType>)_termTypeCrudViewModel.TermTypes;
            set => _termTypeCrudViewModel.TermTypes = value;
        }


        public event EventHandler OnNavigateEvent;
        public string Title => "Term Types";
        public UserControl NavigateToPanel { get; set; }

        public void Dispose()
        {
            _termTypeCrudViewModel.Dispose();
        }
    }
}
<Application x:Class="PresentationWPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:PresentationWPF"
             xmlns:userPanels="clr-namespace:PresentationWPF.ViewModel.Client.UserPanels"
             Startup="Application_Startup">
    <Application.Resources>
        <!--
            Create Month names list to use in ComboBox
        -->
        <userPanels:MonthName x:Key="MonthName" />
        <Style TargetType="ComboBox" x:Key="ComboBoxMonthNamesStyle">
            <Setter Property="DataContext" Value="{StaticResource MonthName}"/>
            <Setter Property="ItemsSource" Value="{Binding MonthNamesCollection}"/>
            <Setter Property="Width" Value="100"/>
        </Style>
    </Application.Resources>
</Application>
namespace PresentationWPF.ViewModel.Client.UserPanels
{
    public class MonthName 
    {
        private ObservableCollection<string> _monthNamesCollection = new ObservableCollection<string>();

        public ObservableCollection<string> MonthNamesCollection
        {
            get => _monthNamesCollection;
            set => _monthNamesCollection = value;
        }

        public MonthName()
        {
            MonthNamesCollection.Add("January"); //      31 days
            MonthNamesCollection.Add("February"); //     28 days in a common year and 29 days in leap years
            MonthNamesCollection.Add("March"); //        31 days
            MonthNamesCollection.Add("April"); //        30 days
            MonthNamesCollection.Add("May"); //          31 days
            MonthNamesCollection.Add("June"); //         30 days
            MonthNamesCollection.Add("July"); //         31 days
            MonthNamesCollection.Add("August"); //       31 days
            MonthNamesCollection.Add("September"); //    30 days
            MonthNamesCollection.Add("October"); //      31 days
            MonthNamesCollection.Add("November"); //     30 days
            MonthNamesCollection.Add("December"); //     31 days
        }
    }
}
namespace PresentationWPF.View.Converters
{
    public class IndexToMonthConverter : IValueConverter
    {
        private DateTime _dateTime;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert Month in 'value(DateTime)' ==> Index 0 to 11
            if (value is DateTime b)
            {
                _dateTime = b;
                return b.Month - 1;
            }

            return 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert 'value(int)' 0 to 11 ==> Month number
            if(value is int b)
                return new DateTime(1,b + 1,1);

            return _dateTime;
        }
    }
}

不要在组合框样式中设置DataContext属性。这样做会破坏任何基于数据上下文的绑定,如
{Binding StartDate}

<Style TargetType="ComboBox" x:Key="ComboBoxMonthNamesStyle">
    <Setter Property="ItemsSource"
            Value="{Binding MonthNamesCollection, Source={StaticResource MonthName}}"/>
    ...
</Style>

...

Binding StarDate
是一个打字错误。它应该是
Binding StartDate
@Clemens oppss!谢谢我猜这发生在我的许多尝试和失败的案例中!现在,如果我省略
Style
属性并使用ComboBoxItem,它就可以工作了。但是我想知道,如果我仍然想以那种方式使用样式,如何修复它。
{Binding StartDate,…}
要求在当前数据上下文中使用StartDate属性。ComboBox样式将DataContext设置为MonthName的实例,该实例没有该属性。解决方案:不要在样式中设置DataContext。非常感谢!它是活的D(我的意思是工作;))在过去的7到9天里,除了在App.xaml中,我在代码中的任何地方都在做任何事情!!!!你能告诉我你对我问的另外两个问题有什么看法吗!?特别是我的下一个计划是这样做:“当用户选择开始日期五月,结束日期组合框应该显示六月到十二月”作为一个注释,如果开始日期和结束日期只有几个月,使用
DateTime?
作为属性类型似乎很奇怪。只需使用一个字符串,或者可能是一个枚举。然后可以直接绑定ComboBox的SelectedItem属性,而无需使用转换器。
namespace PresentationWPF.View.Converters
{
    public class IndexToMonthConverter : IValueConverter
    {
        private DateTime _dateTime;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert Month in 'value(DateTime)' ==> Index 0 to 11
            if (value is DateTime b)
            {
                _dateTime = b;
                return b.Month - 1;
            }

            return 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert 'value(int)' 0 to 11 ==> Month number
            if(value is int b)
                return new DateTime(1,b + 1,1);

            return _dateTime;
        }
    }
}
<Style TargetType="ComboBox" x:Key="ComboBoxMonthNamesStyle">
    <Setter Property="ItemsSource"
            Value="{Binding MonthNamesCollection, Source={StaticResource MonthName}}"/>
    ...
</Style>