簡體   English   中英

XAML:DependecyObject的屬性在作為附加集合中的一項時不會更新

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

我有一個DependencyObject,它位於附加的依賴項屬性(這是一個集合)中。 出於某種原因,綁定到該對象不起作用。

在我的示例中,我綁定了兩件事,一個基本的附加屬性( 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");
    }
}

MyItem:

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!!");
    }
}

集合非常簡單:

public class Coll : List<MyItem>
{
}

發生更改時,Text屬性將傳播到附加屬性,而不傳播到依賴項屬性。

<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的基類。

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!!");
     }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM