Inherit WPF-UserControl from generic base class - wpf

I've got the error "The Tag "EntityView.MainContent" does not exist in the "clr-namespace:Lvt.Views"."
I want to inherit from a generic base class like the following.
C#-Code:
public class EntityView<T> : UserControl where T :IEntityViewModel
{
// Property with set and get a Dependency Property
public object MainContent {get {...} set{...}
}
XAML-Code:
<local:EntityView x:TypeArguments="viewModels:EntityViewModel" x:Class="Lvt.Views.SpecialEntityView"
<local:EntityView.MainContent>
<Grid></Grid>
</local:EntityView.MainContent>
</local:EntityView>
XAML-CodeBehind:
public partial class SpecialEntityView : EntityView<EntityViewModel>
{
public SpecialEntityView() { InitializeComponent(); }
}
Does anyone know why this error message is displayed?
Thanks, Alex

Related

Attributes and Partial Classes

I'm wondering if it is possible to find classes that have an attribute that is on the partial class implementation? Such that:
public class Test {
}
[Attribute()]
public partial class Test {
}
I cannot put the attribute on the original class because it is a Form.

dependency property registered in generic base class

I am trying to construct a validating textbox control using a generic abstract base class as follows and register Min and Max value properites:
public abstract class ValidatingTextBoxBase<T, C>
: TextBox where T : Icomparable<T>
public static readonly DependencyProperty MinValueProperty =
DependencyProperty.Register("MinValue", typeof(T), typeof(C),
new FrameworkPropertyMetadata(DefaultMinValue));
public T MinValue
{
get { return (T)GetValue(MinValueProperty); }
set { SetValue(MinValueProperty, value); }
}
Then I declare several text box controls:
public class IntegerTextBox : ValidatingTextBoxBase<int, IntegerTextBox>
{
public class DecimalTextBox : ValidatingTextBoxBase<decimal, DecimalTextBox>
{
At runtime this works great however I get a lot of design time errors for
'MinValue' property was already registered by 'DecimalTextBox'
or it will be IntegerTextBox or one of the others depending on which view is open.
Any thoughts/help would be appreciated.

How to change exported property through MEF?

I'm studying MEF, and try to use Export attribute to export a property, and import it in an other class.
But my problem is that I want to change this property and the other class can import a new value.
For example,
[Export]
public class A{
[Import("Notes")]
public string Description{get;set;}
}
[Export]
public class B{
[Export("Notes")]
public string Text{get;set;}
}
I want once I change the Text of class B, the A.Description can get changed too.
So, how can I implement this?
Any good idea?
This approach would work for most reference type but not with string which is immutable. This means that after you change the value of B.Text, the objects referenced by A.Description and B.Text will no longer be the same (you can use Object.ReferenceEquals to test this).
One way to do what you are after using MEF is to export/import a method instead of the property:
[Export]
public class A
{
public string Description { get { return GetDescription(); } }
[Import("GetNotes")]
Func<string> GetDescription;
}
[Export]
public class B
{
public string Text { get; set; }
[Export("GetNotes")]
string GetText()
{
return Text;
}
}
Finally note that there are other ways to do this. The most common in .NET is with events.

Custom Class ToString() Override not showing up in WPF ComboBox

I have a WPF application built with MVVM and am trying to display a custom class in a combobox. I am still getting the Namespace.Asset despite overriding the ToString Method to something easier on the eyes. What am I doing wrong?
XAML code
<ComboBox ItemsSource="{Binding Drivers}" SelectedItem="{Binding SelectedDriver}" Grid.Row="20" Grid.Column="1" Grid.ColumnSpan="3"/>
ViewModel Code
public List<Driver> Drivers
{
get
{
return this.drivers;
}
set
{
this.drivers = value;
this.RaisePropertyChanged("Drivers");
}
}
public Driver SelectedDriver
{
get
{
return this.selectedDriver;
}
set
{
this.selectedDriver = value;
this.RaisePropertyChanged("SelectedDriver");
}
}
One of the custom classes code with overriden ToString
public class ExperimentalDriver : Driver
{
public override DriverResponse GetDriverResponse(double time)
{
... random unrelated code....
}
public override string ToString()
{
return "Experimental Driver";
}
}
You might need to set the ToString() on the base class
Something like:
public class Driver
{
protected string displayName;
public override string ToString()
{
return displayName;
}
}
Then your class constructors for your sub classes would simply set the displayName
public class ExperimentalDriver : Driver
{
public ExperimentalDriver()
{
displayName = "Experimental Driver";
}
}
Ok I figured it out. I have an abstract class "Driver" in that abstract class I added the following code to require all my derived classes to have a ToString() method
public new abstract string ToString();
When I remove it the problem goes away.

Winforms DataBinding

There's a text box and some view model with detail
public class Detail { public string Value {get;set;}}
public class SomeVM
{
public Detail {get;set;}
}
Trying to bind to textbox with this code:
textBox.DataBindings.Add("Text", new SomeVm (), "Detail.Value");
But it says "there's no property to bind". Is there a solution for this problem?
Have you tried:
public string Value
{
get
{
return _someVm.Value;
}
set
{
_someVm.Value=value;
}
}
private readonly SomeVM _someVM=new SomeVM();
...
textBox.DataBindings.Add("Text", _someVm, "Value");

Resources