[英]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.