Caliburn.Micro中Binding出错,如何解决?

问题描述 投票:0回答:1

我的程序其他部分都能正常绑定,但是这部分代码不行。

这是我的视图

 <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.comFoiolagTestProject.git。

谁能帮我把这个功能和Caliburn Micro一起用 =]。

c# wpf mvvm binding caliburn.micro
1个回答
1
投票

正如Pavel所指出的,RelativeSource绑定的是控件本身,而不是它的DataContext. 你需要按照我最初的方法来声明绑定。

<Binding Path="DataContext.Dates" RelativeSource="{RelativeSource AncestorType=Calendar}" />
© www.soinside.com 2019 - 2024. All rights reserved.