XAML: 属性 DependecyObject 在附加 collection 中的项目时不更新

XAML: Property on DependecyObject not updating when it is an item in an attached collection

我有一个 DependencyObject,它位于附加的依赖项 属性 中(即 collection)。由于某种原因,绑定到 object 不起作用。

在我的示例中,我绑定了两个东西,一个基本附加 属性 (local:CollHolder.BasicProperty) 和一个常规依赖 属性 (local:MyItem.MyData) - 两者都被绑定到 TextBox 控件的 Text。 XAML 看起来像这样:

<ListView ItemsSource="{x:Bind Items}">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="x:String">
            <StackPanel x:Name="stack" local:CollHolder.BasicProperty="{Binding ElementName=text, Path=Text}" VerticalAlignment="Center" HorizontalAlignment="Center" >
                <TextBox Text="" x:Name="text"/>
                <local:CollHolder.Coll>
                    <local:MyItem MyData="{Binding ElementName=text, Path=Text}"/>
                </local:CollHolder.Coll>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Text 属性 发生更改时,它们会传播到附加的 属性,但不会传播到依赖项 属性。

CollHolder:

public class CollHolder : DependencyObject
{
    public static readonly DependencyProperty BasicPropertyProperty =
        DependencyProperty.RegisterAttached("BasicProperty", typeof(string), typeof(CollHolder), new PropertyMetadata("", DPC));
    public static readonly DependencyProperty CollProperty =
        DependencyProperty.RegisterAttached("Coll", typeof(Coll), typeof(CollHolder), new PropertyMetadata(null));
    public static Coll GetColl(DependencyObject obj)
    {
        var coll = (Coll)obj.GetValue(CollProperty);
        if (coll == null)
        {
            obj.SetValue(CollProperty, coll = new Coll());
        }

        return coll;
    }

    public static void SetColl(DependencyObject obj, Coll value)
    {
        obj.SetValue(CollProperty, value);
    }


    public static string GetBasicProperty(DependencyObject obj)
    {
        return (string)obj.GetValue(BasicPropertyProperty);
    }

    public static void SetBasicProperty(DependencyObject obj, string value)
    {
        obj.SetValue(BasicPropertyProperty, value);
    }


    private static void DPC(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Debug.WriteLine("Basic Property changed");
    }
}

我的物品:

public class MyItem : DependencyObject
{
    public string MyData
    {
        get { return (string)GetValue(MyDataProperty); }
        set { SetValue(MyDataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyData.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyDataProperty =
        DependencyProperty.Register("MyData", typeof(string), typeof(MyItem), new PropertyMetadata("", DPC));



    private static void DPC(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Debug.WriteLine("CHANGED!!");
    }
}

collection 非常简单:

public class Coll : List<MyItem>
{
}

When changes happen the Text property, they propagate to the attached property, but not to the dependency property.

<TextBox Text="" x:Name="text"/>
<local:CollHolder.Coll>
  <local:MyItem MyData="{Binding ElementName=text, Path=Text}"/>
</local:CollHolder.Coll>

TextBoxlocal:MyItem在同一个DataContext中,可以直接用{x:Bind }获取文本值。

<TextBox Text="{x:Bind }" x:Name="text" />
<local:CollHolder.Coll>
    <local:MyItem  MyData="{x:Bind }" />
</local:CollHolder.Coll>

更新

我试图用Control替换MyItem来测试DependencyProperty是否在Binding模式下工作。它按预期工作。因此,您可以使用 Control 作为 MyItem 的基础 class.

public class MyItem : Control
 {
     public string MyData
     {
         get { return (string)GetValue(MyDataProperty); }
         set { SetValue(MyDataProperty, value); }
     }

     // Using a DependencyProperty as the backing store for MyData.  This enables animation, styling, binding, etc...
     public static readonly DependencyProperty MyDataProperty =
         DependencyProperty.Register("MyData", typeof(string), typeof(MyItem), new PropertyMetadata("", DPC));

     private static void DPC(DependencyObject d, DependencyPropertyChangedEventArgs e)
     {
         Debug.WriteLine("CHANGED!!");
     }
 }