【问题标题】:Error with Binding in Caliburn.Micro, how to solve?Caliburn.Micro 中的绑定错误,如何解决?
【发布时间】:2020-08-16 22:44:18
【问题描述】:

我的程序的其余部分正常绑定,但是这部分代码不起作用:

这是我的观点:

 <Window x:Class="TestProject.Views.MainWindowView"
    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"
    xmlns:local="clr-namespace:TestProject.Views"
    xmlns:cal="http://www.caliburnproject.org"
    mc:Ignorable="d"
    Title="MainWindowView" Height="450" Width="800">

<Window.Resources>
    <local:LookupConverter x:Key="LookupConverter" />

    <Style x:Key="CalendarDayButtonStyle" TargetType="CalendarDayButton">
        <Style.Triggers>
            <DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource LookupConverter}">
                        <Binding />
                        <!--CaliburnMicro does not connect-->
                        <Binding Path="Dates" RelativeSource="{RelativeSource AncestorType=Calendar}" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="Pink" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid Margin="5">

    <Calendar SelectionMode="MultipleRange"
              CalendarDayButtonStyle="{DynamicResource CalendarDayButtonStyle}" />
</Grid>

这是我的转换器:

  public class LookupConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var date = (DateTime)values[0];
        var dates = values[1] as HashSet<DateTime>;
        return dates.Contains(date);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

这是我的 ViewModel:

  internal class MainWindowViewModel : Screen
{
    public MainWindowViewModel()
    {
        Dates.Add(DateTime.Today);
        Dates.Add(DateTime.Today.AddDays(2));
        Dates.Add(DateTime.Today.AddDays(4));
    }

    public HashSet<DateTime> Dates { get; } = new HashSet<DateTime>();
}

我在 GitHub 上托管了这部分有问题的代码:https://github.com/Foiolag/TestProject.git

请有人帮我用 Caliburn Micro 完成这项工作 =]

【问题讨论】:

  • DatesWindow 数据上下文的属性,而不是 Calendar 控件

标签: c# wpf mvvm binding caliburn.micro


【解决方案1】:

正如 Pavel 所指出的,RelativeSource 绑定到控件本身,而不是它的 DataContext。您需要按照我最初提供的方式声明绑定:

<Binding Path="DataContext.Dates" RelativeSource="{RelativeSource AncestorType=Calendar}" />

【讨论】:

    猜你喜欢
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2018-09-01
    • 1970-01-01
    相关资源
    最近更新 更多