WPF: Create a custom control without rewriting the ControlTemplate - wpf

Hey, I am creating a Custom Control i WPF inheriting from the ListView. However, I want it to look exactly as the already existing ListView.
Is there a way To use the default ListView Template in a Custom Control without rewriting it in xaml? I do have a Generic.xaml file with the new control added, but I should no need to rewrite the template code.
Thanks
EDIT: I also want to keep it as DRY as possible without repeating (making a mess) the code.

If you subclass the ListView, them your subclassed control will use the ListView Template. That's it! You do not have to do anything!
The Template used by a control is defined by its DefaultStyleKey dependency property. If you want to change the template of your control, set this property as follows:
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl),
new FrameworkPropertyMetadata(typeof(MyControl)));
However, if you do not set this property, it will use the value set by the superclass.
I think the problem is that you have used "Add New Item" => "Custom Control" to create you control then changed the class it extends. Instead of doing this, just add a new C# class and extend ListView.

<Style TargetType="{x:Type local:MyControl}" BasedOn={StaticResource {x:Type ListView}}" />

Related

How to get (MahApps) metro style in derived control?

I create my own control to look and act like ComboBox.
Approach A: I create UserControl with ComboBox as a content. It HAS the metro style, it looks perfect. It works. However I have to manually recreate ComboBox properties in my control. A lot of redundant, ugly code.
Approach B: I extend ComboBox control itself, so no extra coding is needed. It works as charm, however - it's a ComboBox with a different name, so it's not targeted with the metro style for ComboBox.
How to make my new derived control use the metro style for ComboBox?
Add an implicit style to your App.xaml file (replace CustomComboBox with the name of your derived class):
<Style TargetType="local:CustomComboBox" BasedOn="{StaticResource MetroComboBox}" />
If you are using the Material Design toolkit, it should be:
<Style TargetType="local:CustomComboBox" BasedOn="{StaticResource MaterialDesignComboBox}" />

ListView Custom Control Template reset when adding view

The control resets to the standard ListView in the designer when I declare a view in XAML.
<my1:CustomControl1.View>
<GridView></GridView>
</my1:CustomControl1.View>
I tried doing the same thing in code. The CustomControl1.Template changes from having a TargetType of "CustomControl1" to the default ListView template after I set the view to a new GridView().
The template itself doesn't seem to be the problem - it works if I use it as a local resource. If I can't get this working I could make a UserControl with a templated listview inside but for various reasons I'd like a CustomControl. Any help appreciated.
I found another solution here. You create your own GridView class and have it return the custom control as the default style key. You then use this instead of the standard gridview
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/056df061-0666-4fe2-9fa6-8a6440a23ff0/

Control Template that wraps another control in XAML

I want to create a custom control that extends a built-in control and then has a template that wraps that control with a container?
The C# class:
class ExtraBorderTextBox : TextBox {}
The Xaml:
<ControlTemplate>
<Border>
<TextBox/>
</Border>
</ControlTemplate>
That doesnt' work because the TextBox in the control template isn't my custom control, it is a second instance.
I need access to the properties and events on TextBox, having a different parent doens't make sense, I would have to replicate all of that in my class.
This is a simplified example; imagine Border being replaced with a ContentControl that has a 50 line control template for itself. I guess I want something like ContentPresenter (like I have in the ContentControl), but there isn't anything like a "ControlPresenter". Right? Am I missing something, or am I stuck with replicating my content control for the TextBox, or replicating the TextBox behaviour and presentation for my content control?
Thanks.
Update:
There is an answer here that does what I want, which is to copy the default template for System.Windows.Controls.TextBox. This will do what I want; I can insert my container into that. I was hoping that WPF provided a way that is more maintainable to do something like this, something like a adorner/decorator pattern.
Is there any way to make this better in some way? Would using something like Expression Blend make this so that I don't have to hand-edit the XAML pasted in from the webpage?
You could use the default control template as a base and modify it. The default control templates can be found here: http://msdn.microsoft.com/en-us/library/aa970773.aspx
If I understood you right, you want to inherit from TextBox, do some overriding, and use that new class in XAML.
If so:
1) declare the xmlns namespace at the top of your file:
<UserControl
...
xmlns:local="TheAssemblyWhereExtraBorderTextBoxResides"
...>
2) use your custom textbox:
<ControlTemplate>
<Border>
<local:ExtraBorderTextBox />
</Border>
</ControlTemplate>

WPF binding to a non dependency property on a control template

I'm creating a WPF custom control as an auto learning exercise. My control has a ListView inside the template. I wanto my control user be able on defining the needed columns in his own Xaml, but I did not get the strategy on how to pass the columns to the inner listview since binding with FindAncestor complain that "Columns" is not a DependencyProperty.
Wekk the questions are:
How to achieve bind a property from xaml to the template when it is not a DP
Correct my design: I think there is something wrong: if someone would change completely my template, how should I let him use the Column collection ?
why not inherit from ListView directly? Then you have all the properties you need for the ListView and can also add you own properties to the class.
Then you can apply a custom Style to your control to make it look like you want. (Here you have a basic ListView Style that you can use and expand to your needs)
Sometimes binding to a property that is not a dependency property can be solved using the Binding Mode OneWayToSource
Have you tried that?

Silverlight - preventing ChildWindow movement

Anyone got any neat solutions to prevent a Silverlight ChildWindow being moved?
thanks,
Mark
I'm not sure you'd call this neat but...
Create yourself a new Templated control and call it ImmovableChildWindow.
Modify the class it inherits from to be ChildWindow.
Open Themes/generic.xaml you will find an initial style for the ImmoveableChildWindow
In the Silverlight documentation you'll find the existing template for a ChildWindow at ChildWindow Styles and Templates.
Note the existing TargetType value for the ImmovableChildWindow style.
Copy'n' paste the whole default style for a ChildWindow from the documentation into your themes/generic.xaml file.
Replace TargetType for this copy to the same value as the exiting ImmovaleChildWindow style.
You can now delete the initial style. Leave only the large copy of ChildWindow style now targeting ImmovableChildWindow.
Find within the Template setter change the TargetType of to the same value as the style TargetType
Search through the template and find a Border with the name Chrome. Delete the x:Name="Chrome" attribute. (This is what we are really after).
Now when you create a new ChildWindow item it will by default inherit form ChildWindow, if you want it to be immovable you need modify it to inherit from ImmovableChildWindow instead (change the base type in the code-behind and the root tag name in the xaml).
The ChildWindow attaches events to the FrameWorkElement with the name "Chrome" which enables the child window to be moved about. However being a well-behaved templated control, if it can't find a FrameworkElement called "Chrome" it just continues to work without that feature.
Not Required to Create new class, instead
Copy the style from: http://msdn.microsoft.com/en-us/library/dd833070%28VS.95%29.aspx
Give x:key="stylename"
In Construtor of Childwindow, paste following code before InitializeComponent:
this.Style = App.Current.Resources["childWindow"] as Style;
above solution resolved my issue
Maybe you can try this simple way to do that:
Create a Grid to warp all the content in your ChildWindow.
<Grid Margin="0">
<!--Your ChildWindow. Canvas, Grid, Textblock...Whatever-->
</Grid>
Since the Grid has a 0 margin, you can not click it and move it.

Resources