![](/img/trans.png)
[英]XAML Gridview is not updating when property of item scource changes
[英]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>
TextBox
和local: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.