2010-04-22 245 views
0

我想將WPF數據網格中的文本塊文本綁定到依賴項屬性。不知何故,沒有顯示,但當我在網格外使用相同的文本塊綁定時,一切正常。下面是我的代碼,依賴屬性WPF Grid

 </Grid.RowDefinitions> 

     <StackPanel Grid.Row="0"> 
      <toolkit:DataGrid Name="definitionGrid" Margin="0,10,0,0" AutoGenerateColumns="False" 
               CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="False" 
               RowHeight="25" FontWeight="Normal" ItemsSource="{Binding Subscription}" 
               ColumnHeaderStyle="{DynamicResource ColumnHeaderStyle}" 
               SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Width="450" 
           ScrollViewer.VerticalScrollBarVisibility="Auto" Height="200">   
        <toolkit:DataGridCheckBoxColumn Header="Email" Width="60" Binding="{Binding ReceivesEmail}" CellStyle="{StaticResource cellCenterAlign}"/> 

        <toolkit:DataGridTemplateColumn Header="Others" Width="220" CellStyle="{StaticResource cellCenterAlign}" IsReadOnly="True"> 
         <toolkit:DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <TextBlock Text="{Binding Path=OtherSubs}"/> 
          </DataTemplate> 
         </toolkit:DataGridTemplateColumn.CellTemplate> 
        </toolkit:DataGridTemplateColumn> 
       </toolkit:DataGrid.Columns> 
      </toolkit:DataGrid> 
      <TextBlock Text="{Binding Path=OtherSubs}"/> 
     </StackPanel> 

代碼隱藏

public string OtherSubs 
{ 
    get { return (string)GetValue(OtherSubsProperty); } 
    set { SetValue(OtherSubsProperty, value); } 
} 
public static readonly DependencyProperty OtherSubsProperty = DependencyProperty.Register("OtherSubs", typeof(string), 
    typeof(ProgramSubscriptions), new UIPropertyMetadata(string.Empty)); 

     //other.... 
     for (int i = 0; i < OtherPrgList.Count; i++) 
     { 
      foreach (int y in myList) 
      { 
       ProgramSubscriptionViewModel otheritem = OtherPrgList[i]; 
       if (y == otheritem.Program.ID) 
        OtherSubs += otheritem.Subscriber.Username + ", "; 
      } 
     } 

請不要讓我知道如果有另一種方式,我可以完成這項工作,而不是使用一個DependencyProperty,althouht測試我做把數據網格下面的文本塊,它工作得很好.. 幫助!

+0

我假設OtherSubs是一個集合中的類的一部分,並且DataGrid的ItemsSource被設置爲這個集合? – 2010-04-22 15:57:58

回答

2

認購屬性必須是ProgramSubscriptions對象的集合。它必須至少支持IEnumerable接口。通常情況下,你會有類似列表<ProgramSubscriptions>。此外,OtherSubs顯然是ProgramSubscriptions,這是好的。

您能否請您說明您如何使用「在網格之外的相同文本塊綁定」?

+0

在網格外部,我使用下面的代碼, 它工作。你能告訴我上面的代碼將工作,我很新的WPF,我不知道如何做到這一點.. – developer 2010-04-22 17:34:25

+0

我的訂閱屬性是ProgramSubscriptions對象obsrvablecollection ..請幫助.. – developer 2010-04-22 18:29:09

+0

那麼,這有點令人困惑。如果上面的TextBlock與DataGrid並排工作,那麼看起來好像TextBlock的和DataGrid的父級DataContext綁定到某個ProgramSubscriptions實例,因此ItemsSource =「{Binding Subscription}」無法工作。哪些類實現了Subscription屬性? 你可以看看Debug窗口嗎?如果有一些綁定錯誤,他們應該在那裏記錄。如果你不能自己破譯它們,請將它們張貼在這裏。 – wpfwannabe 2010-04-22 18:50:38

0

您正在將DataGrid綁定到訂閱。無論DataGrid的DataContext是什麼,這都必須是一個屬性。正如wpfwannabe所說,它應該支持IEnumerable。理想情況下,您將擁有ObservableCollection<>或派生的,因此DataGrid會自動更新。

從那裏DataGrid將獲得它應該顯示的項目。要顯示實際的數據,你有你的DataGridTemplateColumn定義。由於您綁定到OtherSubs,這意味着您的Subscription IEnumerable枚舉的對象應具有該屬性。順便說一句,它不需要是一個依賴屬性,這個工作。

+0

訂閱屬性是一個Observable對象集合.. – developer 2010-04-22 18:21:32

+0

你的問題可能是一個WPF「bug」。參見http://blogs.msdn.com/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-對其- columns.aspx – 2010-04-23 09:49:46