-1

私はそのようなことをする必要があるWPFアプリケーションを持っています:

<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>

私の userControl では、そのDependencyPropertyようなものを作成し (以下を参照) ProductNameOnProductNameChangedコールバックで を設定しました。

TextBlockをバインドするときにユーザーコントロールのが更新されずItemsControl、コールバックが起動されません。

#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
4

2 に答える 2

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)); 
于 2012-04-17T19:53:14.377 に答える
0

Arrrghhhはそれを手に入れました... this.ProductName = "";UserControlのコンストラクターにありました

私はこれに多くの時間を無駄にしました...そしてあなたのすべてを無駄にして申し訳ありません.

あなたのコード Clemens を試してみてわかったので、あなたの回答を「回答済み」とマークします。

みんな、ありがとう。

于 2012-04-17T20:03:28.060 に答える