Ошибка с привязкой в ​​Caliburn.Micro, как ее решить? - PullRequest
0 голосов
/ 02 мая 2020

Остальная часть моей программы связывается нормально, но эта часть кода не работает:

Это мой вид:

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

1 Ответ

1 голос
/ 03 мая 2020

Как указывает Павел, RelativeSource связывается с самим элементом управления, а не с его DataContext. Вам необходимо объявить привязку в том виде, в котором я ее предоставил:

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