2012-04-17 142 views
-1

我有一个WPF应用程序,我需要做这样的事情:WPF的ItemsControl绑定到用户控件

<ItemsControl x:Name="lstProducts"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
     <TextBlock Text="{Binding ProductName}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

我设置的ItemsSource在这样的代码:lstProducts.ItemsSource = MyEFContext.Products;

到目前为止一切正常。现在我想用我自己的UserControl来显示产品,而不是像这样的TextBlock

<ItemsControl x:Name="lstProducts"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
     <my:ProductItemCtl ProductName="{Binding ProductName}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

在我的用户,我创建了一个DependencyProperty那样(见下文),我在OnProductNameChanged回调设置ProductName

在绑定ItemsControl时,我的用户控件中的TextBlock没有更新,并且回调没有启动。

#region ProductName 

     /// <summary> 
     /// ProductName Dependency Property 
     /// </summary> 
     public static readonly DependencyProperty ProductNameProperty = 
      DependencyProperty.Register("ProductName", typeof(String), typeof(ProductItemCtl), 
       new FrameworkPropertyMetadata("", 
        new PropertyChangedCallback(OnProductNameChanged))); 

     /// <summary> 
     /// Gets or sets the ProductName property. This dependency property 
     /// indicates .... 
     /// </summary> 
     public String ProductName 
     { 
      get { return (String)GetValue(ProductNameProperty); } 
      set { SetValue(ProductNameProperty, value); } 
     } 

     /// <summary> 
     /// Handles changes to the ProductName property. 
     /// </summary> 
     private static void OnProductNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      ProductItemCtl target = (ProductItemCtl)d; 
      String oldProductName = (String)e.OldValue; 
      String newProductName = target.ProductName; 
      target.OnProductNameChanged(oldProductName, newProductName); 
     } 

     /// <summary> 
     /// Provides derived classes an opportunity to handle changes to the ProductName property. 
     /// </summary> 
     protected virtual void OnProductNameChanged(String oldProductName, String newProductName) 
     { 
      // Set Product Name in the display Here! 
      this.txtProductName.Text = newProductName; 
     } 

     #endregion 
+1

是否将Mode = TwoWay添加到您的用户控件帮助的绑定上?另外,为什么你在代码中设置文本框的文本(在OnProductNameChanged中)?为什么不绑定它? – 2012-04-17 19:41:56

+1

不是'DependencyProperty.Register(“ProductName”,typeof(String),typeof(AchatItem)''实际上应该有'typeof(ProductItemCtl)'而不是'typeof(AchatItem)'' – 2012-04-17 19:45:13

+0

没有TwoWay不会改变任何东西。我在这里简化了代码,只是为了隔离这个问题,在真正需要做更多的事情,而不仅仅是设置文本值 – danbord 2012-04-17 19:45:41

回答

0

不是答案,但我不能写这个评论。如果您简化这样的代码会发生什么:

public static readonly DependencyProperty ProductNameProperty = DependencyProperty.Register(
    "ProductName", typeof(string), typeof(ProductItemCtl), 
    new FrameworkPropertyMetadata(string.Empty, 
     (o, e) => ((ProductItemCtl)o).txtProductName.Text = (string)e.NewValue)); 
0

Arrrghhh得到它......我在我的用户的构造有this.ProductName = "";

我浪费了太多时间在这...而且浪费遗憾你们所有人。

因为我发现通过尝试你的代码克莱门斯我会将你的答案标记为“已回答”。

谢谢大家。