我有一个类似这样的 AttachedProperty:
Public Class AttachedProperties
Public Shared ReadOnly IconProperty As DependencyProperty = DependencyProperty.RegisterAttached("Icon", GetType(ImageBrush), GetType(AttachedProperties), New FrameworkPropertyMetadata(Nothing, FrameworkPropertyMetadataOptions.AffectsRender))
Public Shared Sub SetIcon(ByVal element As Object, ByVal value As ImageBrush)
element.SetValue(IconProperty, value)
End Sub
Public Shared Function GetIcon(ByVal element As Object) As ImageBrush
Return CType(element.GetValue(IconProperty), ImageBrush)
End Function
End Class
以下是一个类似于这样的ViewModel:
Public Class ViewModel
Public Property ShowingPage as Page
Public Sub New()
ShowingPage = New SamplePage()
End Sub
End Class
我的SamplePage
大致上是这样的:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SamplePage">
<local:AttachedProperties.Icon>
<ImageBrush Source="Pack://..." /> <!-- Page's Icon -->
</local:AttachedProperties.Icon>
</Page>
最后,我有一个使用ViewModel
对象作为ViewModel的视图:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MainWindow">
<StackPanel>
<!-- Showing Icon of page-->
<Image Source="{Binding (AttachedProperties.Icon), Source=<<ShowingPage>>}" />
<!-- Showing content of page -->
<Frame Content="{Binding ShowingPage}" />
</StackPanel>
</Window>
问题是什么?如何将页面图标显示在<<ShowingPage>>的位置?是否可以绑定到
DataContext
上的属性或附加属性?
ShowingPage.(AttachedProperties.Icon)
是不正确的。看起来它是将 AttachedProperty 绑定和通常的绑定结合在一起。 - Hamed Zakery Miab