2009-09-30 73 views
0

我想实现像下面这样(注意Window元素的DataContext属性):将Window.DataContext设置为代码隐藏中的声明属性?

<Window x:Class="Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    DataContext="{Binding MyDataContext}"/> 

Class Window1 
    Public ReadOnly Property MyDataContext() As IEnumerable(Of String) 
     Get 
      Return New String() {"Item1", "Item2"} 
     End Get 
    End Property 
End Class 

回答

1
<Window x:Class="Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    DataContext="{Binding MyDataContext, RelativeSource={RelativeSource Self}}"> 
    <Grid> 
     <ListBox ItemsSource="{Binding}"/> 
    </Grid> 
</Window> 

我想这可能是最好使用一个DependencyProperty,应该同步好。

+0

我一直在努力解决同样的问题。它现在有效。谢谢你的提示。 – 2013-09-01 02:51:18

相关问题