Update binding of property in silverlight
I'm trying to update a property using a PropertyChangedEventHandler
, but I think my conceptual understanding of how this works might be a bit flawed. As I'm new to WPF and silver-light.
So, let me explain, I have a property that is set to 0, but after some time a thread changes the value from 0 to 9 internally, but despite the change in value, this property never gets updated in the actual view and I don't know why! Even after I implement a PropertyChangedEventHandler
there is no change, but if I log the property it shows that the value is in fact 9
So here is t开发者_StackOverflow中文版he snippet of code that implements PropertyChangedEventHandler
:
public class CustomColumn : IColumnViewable, INotifyPropertyChanged
{
...
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void OnPropertyChanged(string propertyName)
{
Foo.log.Error(": start on property change");
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Foo.log.Error(": end on property change");
}
public static string _total;
public string total { get { return _total; } set { _total = value; OnPropertyChanged("total"); Foo.log.Error(": property change"); } }
...
}
Here is part of my xaml:
<DataTemplate x:Key="ColumnView">
<UserControl HorizontalAlignment="Stretch">
<StackPanel HorizontalAlignment="Stretch">
...
<RichTextBox Margin="5,2,5,2">
<Paragraph>
<Run Text="{Binding Path=total, Mode=OneWay}" FontWeight="Bold" FontSize="30" />
<Run Text=" total clicks" FontWeight="Bold" />
</Paragraph>
</RichTextBox>
...
<ContentControl VerticalAlignment="Stretch" Content="{Binding Path=timeline}" ContentTemplate="{Binding Path=timelineView.ContentTemplate}" />
</StackPanel>
</UserControl>
</DataTemplate>
And I do this on initialize:
CustomColumn content = new CustomColumn();
content.total = "0";
And then I pass the object to a thread which at some point does this:
content.total = "9";
Foo.log.Error("value is "+content.total);
And the property never updates and I don't know why - any help is greatly appreciated
If I understand the details of your question, you are updating a UI bound value on a background thread. You need to make that happen on the UI thread or the change will not be visible. In one of our WPF apps random updates were disappearing until we realised this.
We do a lot of multi-threading in our Silverlight (and WPF) apps so to avoid this problem, we implemented our notify helper in a base class like the one below (other stuff trimmed out). It dispatches all notify messages on the main UI thread. Give it a try:
public class ViewModelBase : INotifyPropertyChanged
{
protected delegate void OnUiThreadDelegate();
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
// Ensure property change is on the UI thread
this.OnUiThread(() => this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)));
}
}
protected void OnUiThread(OnUiThreadDelegate onUiThreadDelegate)
{
// Are we on the Dispatcher thread ?
if (Deployment.Current.Dispatcher.CheckAccess())
{
onUiThreadDelegate();
}
else
{
// We are not on the UI Dispatcher thread so invoke the call on it.
Deployment.Current.Dispatcher.BeginInvoke(onUiThreadDelegate);
}
}
}
Your code does not show where you make the object the DataContext
of your controls, which is necessary for your bindings which do not specify another source and hence bind to the DataContext
.
CustomColumn content = new CustomColumn();
content.total = "0";
Do you have any line after this where this object is passed to your view?
精彩评论