Caliburn.Micro中的绑定错误,如何解决? [英] Error with Binding in Caliburn.Micro, how to solve?

查看:81
本文介绍了Caliburn.Micro中的绑定错误,如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The rest of my program binds normally, but this part of the code doesn't work:

这是我的观点:

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

And this is my 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

I hosted this part of the code with the problem on GitHub: https://github.com/Foiolag/TestProject.git

请,有人帮助我使用Caliburn Micro =]

推荐答案

正如Pavel指出的那样,RelativeSource绑定到控件本身,而不是其DataContext.您需要声明我最初禁止使用的绑定:

As Pavel points out, RelativeSource binds to the control itself, not its DataContext. You need to declare the binding as I originally provioded it:

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

这篇关于Caliburn.Micro中的绑定错误,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆