Error with Binding in Caliburn.Micro, how to solve?

≡放荡痞女 提交于 2020-05-17 05:46:56

问题


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

This is my View:

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

This is my converter:

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

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

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

Please, someone help me make this work with Caliburn Micro =]


回答1:


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


来源:https://stackoverflow.com/questions/61559500/error-with-binding-in-caliburn-micro-how-to-solve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!