我在其中一个用户控件中创建了一个依赖项属性,但是需要一些方法将它绑定到另一个XAML(另一个用户控件)。这是C#代码
DateRangeSelectorControl.cs
public static readonly DependencyProperty TodayDateProperty =
DependencyProperty.Register("TodayDate",
typeof(DateTime),
typeof(DateRangeSelectorControl),
new PropertyMetadata(null, TodayDateChanged));
我有另一个XAML(ActivityListMenuControlView.xaml),我需要绑定此属性(TodayDateProperty),以便可以公开它并调用它的回调。 这是XAML代码:
<DateRangeSelector:DateRangeSelectorControl x:Name="DateRangeSelector"
Grid.Column="1"
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
AutomationProperties.AutomationId="AID_TaskListDateRangeSelector"
DateRangeUpdatedCmd="{Binding Path=DateRangeSelectionUpdatedCommand}"
FontSize="{StaticResource TaskListMenuFontSize}"
RangeOptions="{Binding Path=DateRangeSelectionOptions,
Mode=OneTime}"
SelectedDateRange="{Binding Path=SelectedRange,
Mode=TwoWay}"
Visibility="{Binding Path=ShowFilterOptions,
Converter={StaticResource boolToVisibility}}" />
有什么办法吗?
更新: 根据O.R Mapper的建议,我对此XAML(ActivityListMenuControlView.xaml)进行了以下更改:
<DateRangeSelector:DateRangeSelectorControl x:Name="DateRangeSelector" Grid.Column="1" Margin="10 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Center" AutomationProperties.AutomationId="AID_TaskListDateRangeSelector" DateRangeUpdatedCmd="{Binding Path=DateRangeSelectionUpdatedCommand}" FontSize="{StaticResource TaskListMenuFontSize}" RangeOptions="{Binding Path=DateRangeSelectionOptions, Mode=OneTime}" SelectedDateRange="{Binding Path=SelectedRange, Mode=TwoWay}" Visibility="{Binding Path=ShowFilterOptions, Converter={StaticResource boolToVisibility}}" TodayDateProperty="{Binding TodayDate, ElementName=DateRangeSelector}"
我仍然收到错误:预期的财产或事件。编译后,我得到以下错误:
错误2在“DateRangeSelectorControl”类型中找不到属性“TodayDateProperty”。 错误4默认值类型与属性'TodayDate'的类型不匹配。
有什么想法吗?
更新: 正如Sheridan所建议的那样,我将XAML改为:
<DateRangeSelector:DateRangeSelectorControl x:Name="DateRangeSelector" Grid.Column="1" Margin="10 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Center" AutomationProperties.AutomationId="AID_TaskListDateRangeSelector" DateRangeUpdatedCmd="{Binding Path=DateRangeSelectionUpdatedCommand}" FontSize="{StaticResource TaskListMenuFontSize}" RangeOptions="{Binding Path=DateRangeSelectionOptions, Mode=OneTime}" SelectedDateRange="{Binding Path=SelectedRange, Mode=TwoWay}" Visibility="{Binding Path=ShowFilterOptions, Converter={StaticResource boolToVisibility}}" TodayDate="{Binding TodayDate, ElementName=DateRangeSelector}"/>
Wrapper属性“TodayDate”在DateRangeSelector中定义如下:
DateRangeSelector.cs
public DateTime TodayDate { get { return (DateTime)GetValue(TodayDateProperty); } set { SetValue(TodayDateProperty, value); } }
在viewmodel(ActivityListMenuControlViewModel.cs)中,我创建了另一个“TodayDate”(在绑定中指定),如下所示
public DateTime TodayDate { get; set; }
编译时我得到以下错误:
错误2在“DateRangeSelectorControl”类型中找不到属性“TodayDate”。 错误12默认值类型与属性'TodayDate'的类型不匹配。
任何帮助?
答案 0 :(得分:0)
假设该属性属于名为SomeClass
的类,其实例在ActivityListMenuControlView.xaml
中声明,并且被称为SomeProperty
,并假设您显示的Xaml片段是ActivityListMenuControlView.xaml
的一部分,您可以像这样绑定它:
<SomeClass SomeProperty="{Binding TodayDate, ElementName=DateRangeSelector}"/>
答案 1 :(得分:0)
在我看来,好像你的错误告诉你问题是什么:
在'DateRangeSelectorControl'类型中找不到属性'TodayDateProperty'。错误4默认值类型与属性'TodayDate'的类型不匹配。
这是因为您不在您的控件中注册了一个名为DependencyProperty
的{{1}}。因此,请尝试使用您注册的TodayDateProperty
的名称 - DependencyProperty
:
TodayDate
更新&gt;&gt;&gt;
好的,我认为我现在看到了你的问题。您正在尝试将控件中的数据绑定到视图模型属性。您<DateRangeSelector:DateRangeSelectorControl x:Name="DateRangeSelector"
Grid.Column="1"
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
AutomationProperties.AutomationId="AID_TaskListDateRangeSelector"
DateRangeUpdatedCmd="{Binding Path=DateRangeSelectionUpdatedCommand}"
FontSize="{StaticResource TaskListMenuFontSize}"
RangeOptions="{Binding Path=DateRangeSelectionOptions, Mode=OneTime}"
SelectedDateRange="{Binding Path=SelectedRange, Mode=TwoWay}"
Visibility="{Binding Path=ShowFilterOptions,
Converter={StaticResource boolToVisibility}}"
TodayDate="{Binding TodayDate, ElementName=DateRangeSelector}" />
的外观取决于您的视图模型与视图“连接”的方式。假设您的视图模型的实例被设置为视图的Binding.Path
,那么您就可以像这样访问视图模型属性:
DataContext
当然,如果您的控件是在TodayDate="{Binding DataContext.TodayDate, RelativeSource={RelativeSource
AncestorType={x:Type UserControl}}}"
中声明的,那么您需要使用以下语法:
MainWindow