我一直在寻找一段时间,而且我们找不到解决方案。
我的目标是将我的ViewModel中的属性绑定到自定义控件中列表框的选定项目。
首先关闭 - 此日历控件来自Jarloo Custom Calendar。我只是修改它以供个人定制使用。
日历控件(缩写)如下:
<ResourceDictionary>
<Style TargetType="{x:Type Neptune:Calendar}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Neptune:Calendar}">
<!--Some header items and other misc controls....-->
<DockPanel>
<!--Calendar-->
<ListBox ItemsSource="{Binding Days}" Background="{x:Null}"
SelectedItem="{Binding SelectedDay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
和本地(缩写):
public class Calendar : Control
{
public ObservableCollection<Day> Days { get; set; }
//SelectedDay Property
public static readonly DependencyProperty SelectedDayProperty =
DependencyProperty.Register("SelectedDay", typeof(Day), typeof(Calendar),
new PropertyMetadata(null, OnSelectedDayChanged));
public Day SelectedDay
{
get { return (Day)GetValue(SelectedDayProperty); }
set { SetValue(SelectedDayProperty, value); }
}
private static void OnSelectedDayChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
Calendar d = pager as Calendar;
//MessageBox.Show(d.SelectedDaDateString());///THIS SHOWS CORRECT SELECTED DATE!!!!
d.SetValue(ThisDayProperty, d.SelectedDay);
}
在使用此控件的窗口中,我尝试绑定到此SelectedDay属性,甚至使另一个DP将值传递给仅用于测试。这两个值都没有正确绑定。
<Neptune:Calendar Grid.Row="1" x:Name="Calendar" Margin="0,10,0,0"
ThisDay="{Binding DaySelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
我不明白......为什么这不起作用?
我想要做的就是公开一个反映日历列表中所选项目的属性。
答案 0 :(得分:0)