繁体   English   中英

将依赖项属性绑定到另一个XAML中

[英]Binding dependency property into another xaml

我在其中一个用户控件中创建了一个依赖项属性,但需要某种方式将其绑定到另一个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}}" />

有什么办法吗?

更新:根据OR映射器的建议,我对此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}"/> 

包装器属性“ 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'的类型不匹配。

有什么帮助吗?

假设该属性属于一个名为SomeClass的类,该类的一个实例在ActivityListMenuControlView.xaml声明,并且称为SomeProperty ,并且假设您显示的Xaml代码段是ActivityListMenuControlView.xaml的一部分,则可以像这样简单地绑定它:

<SomeClass SomeProperty="{Binding TodayDate, ElementName=DateRangeSelector}"/>

在我看来,您的错误似乎是在告诉您问题所在:

在类型'DateRangeSelectorControl'中找不到属性'TodayDateProperty' 错误4默认值类型与属性'TodayDate'的类型不匹配。

这是因为你没有注册DependencyProperty名为TodayDateProperty在你的控制。 因此,尝试使用您已注册的DependencyProperty的名称TodayDate

<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声明控件,则需要使用以下语法:

TodayDate="{Binding DataContext.TodayDate, RelativeSource={RelativeSource 
    AncestorType={x:Type MainWindow}}}" 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM