Caliburn.Micro 中的绑定错误,如何解决?
Error with Binding in Caliburn.Micro, how to solve?
我程序的其余部分可以正常绑定,但是这部分代码不起作用:
这是我的观点:
<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 完成这项工作 =]
正如 Pavel 指出的那样,RelativeSource 绑定到控件本身,而不是它的 DataContext。您需要按照我最初提供的方式声明绑定:
<Binding Path="DataContext.Dates" RelativeSource="{RelativeSource AncestorType=Calendar}" />
我程序的其余部分可以正常绑定,但是这部分代码不起作用:
这是我的观点:
<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 完成这项工作 =]
正如 Pavel 指出的那样,RelativeSource 绑定到控件本身,而不是它的 DataContext。您需要按照我最初提供的方式声明绑定:
<Binding Path="DataContext.Dates" RelativeSource="{RelativeSource AncestorType=Calendar}" />