CollectionViewSource as Class Property Type - wpf

I have a Class in my main WPF application which has a Property defined in the class as follows:
Public Class AppExample
Public PropertyName As CollectionViewSource
The project solution also inherits a Class Library (separate project but included in the solution and using the Inherits statement) - in the Class Library I want to do the same thing but I get an error.
Public Class ClassLibraryExample
Public PropertyName as CollectionViewSource
this results in:
Type 'CollectionViewSource' is not defined
How do I fix this?

Add the refernce of PresentationFramework.dll to your class library. It has namespace System.Windows.Data which contains CollectionViewSource

When you are using CollectionViewSource you have to use Data namespace(System.Windows.Data).

Related

Generic ReactiveUserControl "cannot be edited in Design view"

I changed my UserControl to be a ReactiveUserControl and now I can't view the Design View. Is there anything I can do to get the designer to work with ReactiveUserControl?
The Visual Studio designer has issues when your control or window directly inherits from a generic class. This was a pretty common issue with WinForms as well. You can work around this issue by defining another non-generic class that sits between the generic ReactiveUserControl and your control:
public partial class MyUserControl : MyUserControlBase
{
public MyUserControl()
{
InitializeComponent();
}
}
public abstract class MyUserControlBase: ReactiveUserControl<MyUserControlViewModel>
{
}
In the XAML, our root object element is defined as the base element (MyUserControlBase) and its class declaration is connected to the partial class defined above (MyUserControl):
<myNameSpace:MyUserControlBase
x:Class="MyNameSpace.MyUserControl"
xmlns:myNameSpace="clr-namespace:MyNameSpace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

LINQ to EF (Entify Framework) does not create partial methods "OnColumnChanged"

I have created a LINQ to EF model (to be used as the model in an mvvm wpf application) but the classes for each table do not contain on[columnname]changed or on[columnname]changing methods. I thought these were auto generated by the framework. I wanted to add to some of my own public partial methods in order to create some data validation as shown in the following page: http://blogs.msdn.com/b/bethmassi/archive/2009/07/07/implementing-validation-in-wpf-on-entity-framework-entities.aspx
The following class is an example of the auto generated code from EF.
Imports System
Imports System.Collections.Generic
Partial Public Class client
Public Property idClient As Integer
Public Property chrFirst As String
Public Property chrLast As String
Public Property chrCompany As String
Public Property chrEmail As String
Public Property chrPhone1 As String
Public Property chrPhone1Ext As String
Public Property chrPhone2 As String
Public Property chrPhone2Ext As String
Public Property chrFax As String
Public Property chrSuite As String
Public Property chrAddess As String
Public Property chrCity As String
Public Property chrProvince As String
Public Property chrCountry As String
Public Property chrPostal As String
Public Property dtCreated As Nullable(Of Date)
Public Property dtUpdated As Nullable(Of Date)
Public Property FTC_Type As Nullable(Of Byte)
Public Overridable Property invoices As ICollection(Of invoice) = New HashSet(Of invoice)
Public Overridable Property jobs As ICollection(Of job) = New HashSet(Of job)
End Class
When I add a partial public class of the same name I can see all the declarations of the original class generated by the EF in the declarations dropdown in the upper right hand corner fo the window in visual studio.
1- Does LINQ to EF work this way?
2- I am using vb.net 4 and visual studio 2012, is there another way of doing this?
Thanks in advance
which template are you using to generate the files?
The Self Tracking Entities are best-suited for use in a client/server WPF application, because they implement INotifyPropertyChanged and allow for disconnected (n-tier) change tracking.

Binding to Class Item

This seems like it should be really basic but I can't seemto get it working.
I have a class file called XMLSource as follows:
Public Class XMLSource
Public Shared BrandItems As New MediaItems
Public Class MediaItems
Inherits ObservableCollection(Of MediaObject)
Implements INotifyPropertyChanged
End Class
End Class
Public Class MediaObject
Public Property Name As String
Public Property Title As String
End Class
The application reads an XML file and stores some items into XMLSource.BrandItems (happens on start-up).
I want to bind a Label control's Content property to XMLSource.BrandItems(0).Name
I tried:
<Label Content="{Binding Source={XMLSource},Path=.BrandItems[0].Src}" FontSize="20"></Label>
But it's not working.
Is it possible to bind directly like this?
You cannot contruct bindings like this, if you write {} that indicates a markup extension, further you cannot have static/shared members in a binding path. I think the correct binding would be:
{Binding [0].Src, Source={x:Static ns:XMLSource.BrandItems}}
x:Static is a markup extension which allows access of static members. (Note that this also allows access of fields unlike the Path which only allows public properties)
Where ns is declared in an xmlns attribute and points to the namespace of your XMLSource class.

Using generic collections in Silverlight user controls

There is an interface:
public interface IFoo {
}
A Silverlight user control has a collection of IFoo instances:
public ObservableCollection<IFoo> Items { get; set; }
There is an abstract class that implements the interface:
abstract public class Foo : IFoo {}
And a class that further derives from that:
public class DerivedFoo : Foo {}
With all of that said, I'm trying to add instances of DerivedFoo into the control's collection via XAML, but I receive an error that DerivedFoo is not of type IFoo and cannot be used in the generic collection.
I did find a post in a forum that said this was a bug in Silverlight 3 but would be fixed (I am using Silverlight 4). Is this still a bug or am I going about this incorrectly?
Update:
My code is at home and I'm at work so I can't post the actual XAML, but from memory it was along the lines of:
<my:Thing>
<my:Thing.Items>
<my:DerivedFoo ... />
</my:Thing.Items>
</my:Thing>
The answer is...
The CollectionChanged event handler for the generic collection made an improper cast during the Add action.

wpf user control base class problem

I am new to WPF and have created a WPF User Control Library
I added a Base class that looks like this
public class TControl : UserControl
{
}
and want all of my controls to inherit from it.
I have a Control called Notification which looks like
public partial class Notification : TControl
{
public Notification()
{
InitializeComponent();
}
Works fine except when ever i recompile the hidden partial class where InitializeComponent() is defined gets regenerated and inherits from System.Windows.Controls.UserControl
this gives me an
Partial declarations of 'Twac.RealBoss.UserControls.Notification' must not specify different base classes
error,
is there anyway to force the generated class to inherit from my base class?
Your XAML file probably has:
<UserControl x:Class="YourNamespace.Notification" .... >
Try changing this to:
<Whatever:TControl x:Class="YourNamespace.Notification" xmlns:Whatever="clr-namespace:YourNamespace" />
The error you are getting is because the use of UserControl in the XAML tells the compiler to produce a partial class inheriting from UserControl, instead of inheriting from your class.
You can completely remove the ": TControl":
public partial class Notification : TControl
{
}
and write:
public partial class Notification
{
}
instead, since the base class is defined in the XAML part, as Paul wrote.

Resources