My Database has a Table Foo with the Property IsTrue. I made a Linq2SQL Class "myData.dbml" for this Database and added the generated Foo-Class to my Datasources bar. I have a Simple Winforms Window with a ToggleButton which should bind to the inverse of IsTrue.
To achieve this I tried to add the Property NotTrue to the myData.dbml but this caused an SQLException, invalid row name "NotTrue". I tried to add the Property Code to the myData.cs like this:
public bool NotTrue {
get {
return !this._IsTrue;
}
set {
if (this._IsTrue == value) {
this.OnIsTrueChanging(!value);
this.SendPropertyChanging();
this._IsTrue = !value;
this.SendPropertyChanged("IsTrue");
this.SendPropertyChanged("NotTrue");
this.OnIsTrueChanged();
}
}
}
But then the Property didnt appear when adding Foo to Datasources even when adding [Bindable(true)]
What worked, was binding to IsTrue and negating the Binding with this:
private void InvertBoolBinding(ControlBindingsCollection collection,string property) {
foreach (Binding b in collection) {
if (b.PropertyName.Equals(property)) {
ConvertEventHandler invert = new ConvertEventHandler((sender, e) => { e.Value = !((bool)e.Value); });
b.Format += invert;
b.Parse += invert;
}
}
}
Called like this:
InvertBoolBinding(lockToggleButton.DataBindings, "IsChecked");
Though in my main project there's a huge amount of those Bindings, this approach is quite a hassle. It seems that Datasources isn't able to connect properties through partial classes. When I add the property to the myData.designer.cs, it is found. But this class get regularly regenerated and my property gets lost.
So maybe I need a completely different approach or maybe theres a way to make partial class extensions work. I hope you got some idea on that. Thanks!
Moving the Linq2SQL class to a different assembly makes Visual Studio resolve the partial connection.
Found here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.