简体   繁体   English

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

[英]Binding dependency property into another xaml

I have created a dependency property in one of the usercontrols but need some way to bind it into another XAML(another usercontrol).Here is the C# code 我在其中一个用户控件中创建了一个依赖项属性,但需要某种方式将其绑定到另一个XAML(另一个用户控件)中。这里是C#代码

DateRangeSelectorControl.cs DateRangeSelectorControl.cs

public static readonly DependencyProperty TodayDateProperty =
    DependencyProperty.Register("TodayDate",
    typeof(DateTime),
    typeof(DateRangeSelectorControl),
    new PropertyMetadata(null, TodayDateChanged));

I have another XAML(ActivityListMenuControlView.xaml) where i need to bind this property(TodayDateProperty) so that it can be exposed and it's callback called. 我还有另一个XAML(ActivityListMenuControlView.xaml),在这里我需要绑定此属性(TodayDateProperty),以便可以公开它并调用其回调。 Here is the XAML code: 这是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}}" />

Is there any way? 有什么办法吗?

UPDATE: As per the suggestion from OR Mapper, i have made the following change into this XAML(ActivityListMenuControlView.xaml): 更新:根据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}" 

Still i get an error : Property or event expected. 我仍然收到一个错误:属性或事件预期。 Upon compilation i get the following errors: 编译后出现以下错误:

Error 2 The property 'TodayDateProperty' was not found in type 'DateRangeSelectorControl'. 错误2在类型'DateRangeSelectorControl'中找不到属性'TodayDateProperty'。 Error 4 Default value type does not match type of property 'TodayDate'. 错误4默认值类型与属性'TodayDate'的类型不匹配。

Any idea? 任何想法?

UPDATE: As suggested by Sheridan, i changed the XAML to something like: 更新:如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 property "TodayDate" is defined in DateRangeSelector as below: 包装器属性“ TodayDate”在DateRangeSelector中定义如下:

DateRangeSelector.cs DateRangeSelector.cs

  public DateTime TodayDate { get { return (DateTime)GetValue(TodayDateProperty); } set { SetValue(TodayDateProperty, value); } } 

And in the viewmodel(ActivityListMenuControlViewModel.cs) i created another "TodayDate"(as specified in the binding) as belowL 然后在viewmodel(ActivityListMenuControlViewModel.cs)中创建了另一个“ TodayDate”(在绑定中指定),如下所示

  public DateTime TodayDate { get; set; } 

On compilation i get the below errors: 在编译时,出现以下错误:

Error 2 The property 'TodayDate' was not found in type 'DateRangeSelectorControl'. 错误2在类型'DateRangeSelectorControl'中找不到属性'TodayDate'。 Error 12 Default value type does not match type of property 'TodayDate'. 错误12默认值类型与属性'TodayDate'的类型不匹配。

Any help? 有什么帮助吗?

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

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

It seems to me as though your error is telling you what the problem is: 在我看来,您的错误似乎是在告诉您问题所在:

The property 'TodayDateProperty' was not found in type 'DateRangeSelectorControl' . 在类型'DateRangeSelectorControl'中找不到属性'TodayDateProperty' Error 4 Default value type does not match type of property 'TodayDate'. 错误4默认值类型与属性'TodayDate'的类型不匹配。

This is because you did not register a DependencyProperty named TodayDateProperty in your control. 这是因为你没有注册DependencyProperty名为TodayDateProperty在你的控制。 So instead, try using the name of the DependencyProperty that you did register - TodayDate : 因此,尝试使用您已注册的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}" />

UPDATE >>> 更新>>>

Ok, I think that I see what your problem is now. 好的,我想我明白您的问题了。 You are trying to data bind from your control to a view model property. 您试图将数据从控件绑定到视图模型属性。 What your Binding.Path will look like will depend on how your view model is 'connected' to your view. Binding.Path外观将取决于视图模型如何“连接”到视图。 Assuming that an instance of your view model is set as the DataContext for your view, you would then be able to access the view model property like this: 假设将视图模型的实例设置为视图的DataContext ,则可以像下面这样访问视图模型属性:

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

Of course, if your control is declared in MainWindow instead, then you would need to use the following syntax: 当然,如果改为在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