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

2nd Binding property of ComboBox pointing to its first property binding object

我试了 1 周解决这个问题,但我做不到!我正在搜索和阅读许多页面,包括这些:

(www.thomaslevesque.com) [WPF] HOW TO BIND TO DATA WHEN THE DATACONTEXT IS NOT INHERITED

(Whosebug) How do I use WPF bindings with RelativeSource?

(Whosebug) WPF - Binding error in DataGridComboboxColumn

(Whosebug) WPF Error 40 BindingExpression path error: property not found on 'object'

我要做什么!?

我在 SQL LocalDb 中有一个域 class 和一个 table,名称为 TermType(您可以在下面查看其代码),具有 5 个属性:

我的应用读取 TermType table 并且它应该像这样在 DataGrid 中显示它们:

但它不显示每个术语类型的 Start/End 日期,因为它无法以正确的方式绑定 StartDate/EndDate 属性!而且我在 Output window 收到此错误消息(不作为例外):

System.Windows.Data Error: 40 : BindingExpression path error: 'StartDate' property not found on 'object' ''MonthName' (HashCode=38847270)'. BindingExpression:Path=StartDate; DataItem='MonthName' (HashCode=38847270); target element is 'ComboBox' (Name=''); target property is 'SelectedIndex' (type 'Int32')

话不多说!请检查我的 UserControl xaml 与此相关的文件 window:

<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>

我知道 ComboBox 的问题出在它的第二个 属性 绑定 SelectedIndex 中,它试图在其中找出 'StartDate' 属性 MonthName 对象,而不是 DataGrid ItemsSource="{Binding TermTypes}" 中的 TermTypes。但我不知道该怎么做!?即使我尝试在 SelectedIndex 属性 绑定中使用 RelativeSource,但我可能无法正确使用它!

如果在 Visual Studio 2017 我将鼠标悬停在 ComboBoxStartDate 上,它也会显示此消息:

无法在 'PresentationWPF.View.UserPanels.TermTypeCrudViewModel'

类型的数据上下文中解析 属性 'StartDate'

我什至尝试使用 Freezable class(正如我在第一个 link 中所建议的那样)并且如您所见,我评论了 DataGrid.Resources 行,但即使我尝试在 ComboBoxSelectedIndexBindingProxy 对象的 Data 属性 处使用它,不要指向 TermTypes.

注意: 我尝试在 xaml 中的 ComboBox 中直接插入月份名称(通过使用 ComboBoxItem) 并像下面的代码那样省略 Style 绑定并且它起作用了,但是我仍然想知道如何在我以这种方式使用 Style 绑定时修复代码:

<ComboBox  Grid.Column="1" Grid.Row="0" 
           SelectedIndex="{Binding StartDate,
                            Converter={StaticResource IndexToMonthConverter}}"
>
    <ComboBoxItem>January</ComboBoxItem>
    <ComboBoxItem>February</ComboBoxItem>
    <ComboBoxItem>March</ComboBoxItem>
    <ComboBoxItem>April</ComboBoxItem>
    <ComboBoxItem>May</ComboBoxItem>
    <ComboBoxItem>June</ComboBoxItem>
    <ComboBoxItem>July</ComboBoxItem>
    <ComboBoxItem>August</ComboBoxItem>
    <ComboBoxItem>September</ComboBoxItem>
    <ComboBoxItem>October</ComboBoxItem>
    <ComboBoxItem>November</ComboBoxItem>
    <ComboBoxItem>December</ComboBoxItem>
</ComboBox>

我会很感激告诉我如何解决这个问题!?

我是否以正确的方式使用了 MVVM 和 UnitOfWork!?

有什么更好的建议来替换 App.xaml 中的 MonthName class 或 ComboBox Style!?

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

如果您需要 see/know 关于我的其他相关 classes 和...它们是:

术语类型Class:

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; }
    }
}

(型号)TermTypeCrudService Class:

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();
        }
    }
}

(ViewModel) TermTypeCrudViewModel Class:

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));
        }
    }
}

(查看 - 后面的代码)class:

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();
        }
    }
}

App.xaml 文件:

<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>

月名class:

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
        }
    }
}

我在 ComboBox SelectedIndex:

中使用的转换器 class
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 属性。这样做会破坏任何基于 DataContext 的绑定,例如 {Binding StartDate}.

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