开发者

How to use PropertyChanged to pass trough DataTemplate?

Question is simple: how can I trigger a change on the dataObject without acutaly changing the dataObject, and see this change on the visual?

DataObject:

ProductData : INotifyPropertyChanged
{
    private ProductPartData myProductPartData;

    public ProductPartData ProductPartData
    {
        get
        {
            return myProductPartData;
        }
        set
        {
            if (value != myProductPartData)
            {
                myProductPartData = value;
                OnNotifyPropertyChanged("ProductPartData");
            }
        }
    }
}

DataTemplate:

 <DataTemplate
    DataType="{开发者_开发问答x:Type ProductData}"
    >
    <VisualProduct
        ProductPartData="{Binding Path=ProductPartData, Mode=OneWay}"
        />
</DataTemplate>

And now in a VM I have:

product.OnNotifyPropertyChanged("ProductPartData");

Problem: Even if the getter for ProductPart is called when I execute OnNotifyPropertyChanged, the visual is not notified, because is the same instance of the ProductPartData.

How do I trigger a change seen by the Visual without changing the instance? Thank you,


Daniel,

A solution is to use UpdateTarget() method of the BindingExpression class, this way the target of the binding gets refreshed no matter what; of course, your converter will also be hit - if any. Since I'm guessing you don't have access to your visual in the Product, you could use an attached property and in its callback, you can get the BindingExpression and call UpdateTarget() on it.

Note that I'm using a simple TextBlock as the visual of the data object.

public class BindingHelper
{
    public static bool GetRefreshBinding(DependencyObject obj)
    {
        return (bool) obj.GetValue(RefreshBindingProperty);
    }

    public static void SetRefreshBinding(DependencyObject obj, bool value)
    {
        obj.SetValue(RefreshBindingProperty, value);
    }

    // Using a DependencyProperty as the backing store for RefreshBinding.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty RefreshBindingProperty =
        DependencyProperty.RegisterAttached("RefreshBinding", typeof(bool), typeof(BindingHelper), new UIPropertyMetadata(false, OnRefreshBindingPropertyChanged));

    static void OnRefreshBindingPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs ea)
    {
        TextBlock elem = o as TextBlock;
        if (elem != null)
        {
            BindingExpression bEx = elem.GetBindingExpression(TextBlock.TextProperty);
            if (bEx != null)
            {
                bEx.UpdateTarget();
            }
        }
    }
}

Also, in your data object that you can create a new bool property(let's name it ShouldRefresh) that is bound to the attached property within the template - this will trigger the AP's property changing:

<DataTemplate DataType="{x:Type local:ProductData}">
        <TextBlock Text="{Binding Path=Name, Converter={StaticResource BlankConverter}}"
                   local:BindingHelper.RefreshBinding="{Binding Path=ShouldRefresh}"/>
    </DataTemplate>

So, this way, whenever you want to update the target through binding, you can set:

ShouldRefresh = !ShouldRefresh

in your data class.

HTH.


If you raise a PropertyChanged event and the new value of the property is equal to the value WPF already has, it will simply ignore you. You have a couple of options:

The "fast" way is to set the property to null and then back to the correct value again, ensuring PropertyChanged events are raised each time. It's dirty but it works every time.

The "right" way is to force a binding refresh as discussed in this post by Jaime Rodriguez. Because your visual is data-templated though getting the "dependencyObject" to pass into the call in that post is a little tricky. You may end up needing to use the template's FindName method as discussed in this post by Josh Smith.


We encountered this kind of issue with data coming from a database and converted to a DTO (data transfert object).

Our base class for DTO override Object's method such as Equals() and GetHashCode() as follow:

public override Boolean Equals(Object obj)
{
    // Null reference
    if (null == obj)
        return false;

    // Same reference
    if (Object.ReferenceEquals(this, obj))
        return true;

    EntityDTOBase<TEntity> entiteObj = obj as EntityDTOBase<TEntity>;
    if (null == entiteObj)
        return false;
    else
        return Equals(entiteObj);
}

public Boolean Equals(EntityDTOBase<TEntity> other)
{
    // Null reference
    if (null == other)
        return false;

    // Same reference
    if (Object.ReferenceEquals(this, other))
        return true;

    // No Id: cannot be compared, return false
    if (this.id == TypeHelper.DefaultValue<long>())
        return false;

    // Id comparison
    if (this.id != other.id)
        return false;

    return true;
}

public override Int32 GetHashCode()
{
    return this.id.GetHashCode();
}

So the problem was when we load again the same entity from the database, since the ID is the same, some binding were not properly updated.

This particular issue was circumvented by adding an additional virtual EqualsExtended() method which default implementation simply returns true:

protected virtual Boolean EqualsExtended(EntityDTOBase<TEntity> other)
{
    return true;
}

public Boolean Equals(EntityDTOBase<TEntity> other)
{
  /// Same code as before (except last line):
  return EqualsExtended(other);
}

Now in any implementation of our DTO class we can add some logic to make Equals() returning false in some situations, for example by adding a timestamp when data is retrieved from the database :

protected override Boolean EqualsExtended(EntityDTOBase<Act> other
{
    if (this.Timestamp != other.Timestamp)
    {
        return false;
    }
    return true;
}

Long story short, one way to workaround this issue is to make your class instance look different whenever you want the GUI to update accordingly.


The problem might be that you are returning GuiProductPartData typed myProductPartData with ProductPartData typed ProductPartData? But in any case this shouldn't be like this :)

Also it's not a great practice to have the variable name same as the type, so you shouldn't have a ProductPartData ProductPartData property.


Naming conventions aside (and assuming just typos on the typing) the problem probably resides inside your ProductPartData class. Does it implement INotifyPropertyChanged as well?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜