2011-05-05 61 views
0

我錯過了什麼應該是一個直接的約束力,我希望有人能指出我在正確的方向。Silverlight:爲網格設置數據上下文或UIElement不工作

我有有一個名爲PeopleView模型的視圖模型,它包含了一個人(姓名地址等) 頁面是由一個用戶控件,一個文本框和一個GridView的數量或性質的SL應用

用戶控件代碼

 <frm:LabelFieldContainer Width="145" 
          Height="42" 
          Margin="12,12,0,0" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Top" 
          MyContent="{Binding FirstName}" 
          MyLabel="{StaticResource lblFirstName}" /> 
    <frm:LabelFieldContainer Width="178" 
          Height="42" 
          Margin="166,12,0,0" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Top" 
          MyContent="{Binding LastName}" 
          MyLabel="{StaticResource lblLastName}" /> 
</Grid> 

此容器是由結合到一個資源字典拉標籤名稱,然後內容字段將被綁定到的屬性從我的視圖模型返回(其他用戶控制的爲myContent)。這裏的目標是創建一個通用控件,顯示集合中的標籤名稱和值。即。名字:John目前,該標籤在綁定資源字典時起作用,但值(MyContent)的綁定不會返回任何內容。

這裏是MainPage.xaml中

<Grid x:Name="LayoutRoot" Background="White"> 
    <uc:AddressContainer /> 
    <TextBlock Width="200" 
       Height="50" 
       Margin="101,71,99,179" 
       FontSize="12" 
       Foreground="Black" 
       Text="{Binding LastName}" /> 
    <telerik:RadGridView Name="GridView1" Margin="0,140,0,6" 
         HorizontalAlignment="Stretch" 
         VerticalAlignment="Stretch" 
         AutoGenerateColumns="True" 
         IsFilteringAllowed="True"> 
    </telerik:RadGridView> 

的XAML和背後MainPage.xaml中的代碼是

public partial class MainPage : UserControl 
{ 
    //binding of view model to user control 
    public PeopleViewModel PeopleView 
    { 
     get { return (PeopleViewModel)GetValue(PeopleViewProperty); } 
     set { SetValue(PeopleViewProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for PeopleView. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty PeopleViewProperty = 
     DependencyProperty.Register("PeopleView", typeof(PeopleViewModel), typeof(MainPage), new PropertyMetadata(new PeopleViewModel())); 


    public MainPage() 
    { 
     InitializeComponent(); 
     this.LayoutRoot.DataContext = PeopleView.AllPeople; //tried this but no luck 
     this.GridView1.ItemsSource = PeopleView.AllPeople; //bound to a grid view for testing returns 

    } 

我插入一個網格只是用於測試和返回值的罰款。但是,我試圖將主視圖的DataContext綁定到集合,以便我可以在主控頁面中添加的usercontrol或測試文本塊中填充綁定字段。

有人能指出我正確的方向如何正確綁定UIElements到ItemsSource之外的集合。我一直對網格或列表框項目沒有任何問題,但現在試圖代表某些東西(UI中的單個字段),我陷入困境。 UPDATE:

很短的心理時隔我relaized,因爲我是有約束力的集合我會放在用戶控件到這裏一個列表框後,更新的炫魅XAML

<Grid x:Name="LayoutRoot" Background="White"> 
    <ListBox ItemsSource="{Binding}" Margin="0,0,0,166"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <uc:AddressContainer /> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    <telerik:RadGridView Name="GridView1" Margin="0,140,0,6" 
         HorizontalAlignment="Stretch" 
         VerticalAlignment="Stretch" 
         AutoGenerateColumns="True" 
         IsFilteringAllowed="True"> 
    </telerik:RadGridView> 

和我更新內的數據上下文MainPage.xaml.cs中文件

 public MainPage() 
    { 
     InitializeComponent(); 
     this.DataContext = PeopleView.AllPeople; //updated here 
    } 

我得到的記錄準確的回報(2)在列表框中然而結合仍沒有采取用戶CONT內發生rol(內容字段)顯示集合中返回的值

任何指針仍將被讚賞。

+0

嘗試在DataTemplate內部放置一個簡單的'TextBlock'('」')而不是'AddressContainer',看它是否有效。 – decyclone 2011-05-05 15:59:31

+0

謝謝decyclone。我試過在xaml文件的根目錄下,它不會在那裏顯示值。 – rlcrews 2011-05-05 16:52:42

回答

2

找到了答案。在之前的測試中,我在UserControl中爲基本模型設置了一個數據源。不要問爲什麼...對接綁定重寫正在從主頁面設置的綁定。刪除該數據引用後,UI現在將用戶控件正確綁定到集合。

相關問題