Reputation: 15434
I have following class as the main window:
public partial class Window1 : Window
{
public Network network { get; set; }
public DataSet data_set { get; set; }
public Training training { get; set; }
public string CurrentFile { get; set; }
public MGTester tester;
public MCTester testerSSE;
public ChartWindow ErrorWindow;
public ChartWindow TimesWindow;
public Window1()
{
network = new Network();
data_set = new DataSet();
training = new Training();
tester = new MGTester();
testerSSE = new MCTester();
CurrentFile = "";
ErrorWindow = new ChartWindow();
TimesWindow = new ChartWindow();
InitializeComponent();
for (int i = 0; i < tester.GetDeviceCount(); ++i)
{
DeviceComboBox.Items.Add(tester.GetDeviceName(i));
}
}...
And in my xaml code I have:
<ListView Grid.Row="0" x:Name="NetworkListview" ItemsSource="{Binding network.Layers}" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="layer name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Width="60" Header="neurons" CellTemplate="{StaticResource NeuronsTemplate}"/>
<GridViewColumn Width="110" Header="activation" CellTemplate="{StaticResource ActivationTemplate}"/>
</GridView>
</ListView.View>
</ListView>
Anyway I am binding to some member of Window1... And it works fine but I want to change the member that controlls are binded to - I mean I want to do somthing like that in Window1
this.network = new Network();
When I do this binding stops working - How I can easily and nicely just "refresh" the binding?
Upvotes: 0
Views: 5777
Reputation: 18068
If your binding source is not a UI-class, use a notifying property instead of an auto one.
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
public class MyClass : INotifyPropertyChanged
{
private Network _network;
public Network Network
{
get
{
return _network;
}
set
{
if (value != _network)
{
_network = value;
NotifyPropertyChanged(value);
}
}
}
protected NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
If your source class is a UI-class, define Network to be a DependencyProperty:
http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.aspx
Example quoted from above link:
public class MyStateControl : ButtonBase { public MyStateControl() : base() { } public Boolean State { get { return (Boolean)this.GetValue(StateProperty); } set { this.SetValue(StateProperty, value); } } public static readonly DependencyProperty StateProperty = DependencyProperty.Register( "State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false)); }
Upvotes: 2
Reputation: 244777
There is a way to refresh the binding, but most of the time, there are better ways to it: either make your property into a dependency property, or put the data into another class that implements the INotifyPropertyChanged
interface.
Upvotes: 0